您的位置:首页 > 编程语言 > Java开发

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

2008-11-11 09:03 477 查看
 java和jsp文件的编码都是UTF-8 的

文件下载

    private String inputPath;<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

   

    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 >
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息