struts2文件上传下载及乱码问题解决

 java和jsp文件的编码都是UTF-8 的

文件下载

    private String inputPath;

   

    public void setInputPath(String value) throws UnsupportedEncodingException {

        inputPath =  new String(value.getBytes("ISO-8859-1"),"UTF-8");

    }

   

    public InputStream getInputStream() throws Exception {

    if(log.isDebugEnabled()){

        log.debug("inputPath="+inputPath);

    }

        return new java.io.FileInputStream(inputPath);

//        return ServletActionContext.getServletContext().getResourceAsStream(inputPath);

        //使用上面的语句将无法获取到inputStream,如果资源恰好在web应用下面,则是可以的。

    } 

    /**

     * 下载日志文件

     * @return

     * @throws Excepiton

     */

    public String download () throws Exception{

       String fileName = inputPath.substring(inputPath.lastIndexOf(java.io.File.separator)+1, inputPath.length());

    if(log.isDebugEnabled()){

        log.debug("fileName="+fileName);

    }

        ServletActionContext.getResponse().setHeader("Content-Disposition", "attachment; filename="+new String(fileName.getBytes("gb2312"),"iso-8859-1"));

       return SUCCESS;

    }

注意:第一个转换,"ISO-8859-1"————"UTF-8"  UTF-8是根据你自己的编码来处理
第二个转换,"gb2312"————"iso-8859-1"  你就不要改变了,不管你是什么编码,都这么处理就是了,只要你的客户用的是中文的操作系统,呵呵

配置文件的内容

       <action name="download" class="com.work.qxgl.loglogin.LogLoginAction"

           method="downloadLogs">

           <result name="success" type="stream">

                <param name="inputName">inputStream</param>

                <param name="bufferSize">4096</param>

            </result>

       </action> 

下载设置

<a href='<s:url action="download" namespace="/qxglloglogin"><s:param name="inputPath" value="filePath" /></s:url>'><s:property value="fileName" /></a>

文件上传

    public File getUpload() {

       return upload;

    }

 

    public void setUpload(File upload) {

       this.upload = upload;

    }

 

    // since we are using <s:file name="upload" .../> the file name will be

    // obtained through getter/setter of <file-tag-name>FileName

    /**

     * 如果表单域为fj,那么就叫getFjFileName;

     * @return

     */

    public String getUploadFileName() {

       return fileName;

    }

 

    public void setUploadFileName(String fileName) {

       this.fileName = fileName;

    }

 

    // since we are using <s:file name="upload" ... /> the content type will be

    // obtained through getter/setter of <file-tag-name>ContentType

    /**

     * 如果表单域为fj,那么就叫getFjContentType

     * @return

     */

    public String getUploadContentType() {

       return contentType;

    }

 

    public void setUploadContentType(String contentType) {

       this.contentType = contentType;

    }

在配置文件中限制上传文件的格式

< action name ="fileUpload" class ="tutorial.FileUploadAction" >
           
< interceptor-ref name ="fileUpload" >
               
< param name ="allowedTypes" >
                    image/bmp,image/png,image/gif,image/jpeg
               
</ param >

            </ interceptor-ref >

            < interceptor-ref name ="defaultStack" />            

            < result name ="input" > /FileUpload.jsp </ result >

            < result name ="success" > /ShowUpload.jsp </ result >
       
</ action >

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值