为了账号安全,请及时绑定邮箱和手机立即绑定

点击下载提示java.io.FileInputStream cannot be cast to java.lang.String

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
    //Action
    private List<File> upload;
     
    private List<String> uploadContentType;
     
    private List<String> uploadFileName;
     
    private String result;
 
    private String inputPath;
     
    private String filename;
     
    set/get方法省略
     
    @Override
    public String execute() throws Exception {
        if(upload==null){
            result = "请选择文件";
            return INPUT;
        }
        String path  =  ServletActionContext.getServletContext().getRealPath("/images");
        System.out.println(path);
        File file = new File(path);
        if(!file.exists()){
            file.mkdirs();
        }
        int i=0;
        //循环将批量上传的文件保存到本地
        for (i = 0; i < upload.size(); i++) {
            FileUtils.copyFile(upload.get(i), new File(file,uploadFileName.get(i)));
        }
        System.out.println("共上传了:"+i+"个文件");
        result="上传成功";
        return SUCCESS;
    }
     
    /*
     * 下载action
     */
    public InputStream getDownload() throws IOException{
        String path = ServletActionContext.getServletContext().getRealPath("/images/");
        String filepath = path+filename;
        System.out.println("路径为:"+filepath);
        File file = new File(filepath);
        //像页面输出流
        return FileUtils.openInputStream(file);
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<!-- 配置临时文件位置 -->
<constant name="struts.multipart.saveDir" value="/tmp"/>
<!-- 配置国际化信息 对应app是指配置文件的基础名称 里面key为异常信息,value为提示信息 -->
<constant name="struts.custom.i18n.resources" value="app"></constant>
    <package name="uploadAndDownload" extends="struts-default">
        <action name="uploadAndDownload3" class="com.imooc.action.BatchUploadAndDownloadAction">
            <result>/03.jsp</result>
            <result name="input">/03.jsp</result>
            <!-- 配置拦截器 -->
        <interceptor-ref name="fileUpload">
                <param name="allowedTypes">image/jpeg,image/png</param>
                <param name="maximumSize">2M</param>
        </interceptor-ref>
        <!-- 配置默认拦截器  如果不配置则获取不到file-->
          <interceptor-ref name="defaultStack"></interceptor-ref
        </action>
        <action name="download3" class="com.imooc.action.BatchUploadAndDownloadAction" method="getDownload">
            <param name="inputPath">/images/img2-lg.jpg</param>
            <result type="stream">
                <param name="contentType">application/octet-stream</param>
                <!-- 由getDownloadFile()获得inputStream -->
                <param name="inputName">download</param>
                <param name="contentDisposition">attachment;filename="${filename}"</param>
                <param name="bufferSize">8192</param>
            </result>
        </action>
    </package>
</struts>
1
2
  //页面
  <a href="download3?filename=img2-lg.jpg">测试.jpg</a>&nbsp;&nbsp;${error}


http://img1.sycdn.imooc.com//582ecdaf0001ce4609080674.jpg


没有找到到底错在哪里,点下载就报错,找了好久没找到原因啊~~~

正在回答

代码语言

1 回答

找到原因了 ,在编写action配置的时候,对应action指向了method,返回的InputSteam无法被转换成String,所以按照教程,修改action配置即可。

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

点击下载提示java.io.FileInputStream cannot be cast to java.lang.String

我要回答 关注问题
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号