zero_130
zero_130
采纳率0%
2018-07-23 11:01 浏览 698

[新手]关于struts2文件上传怎么解决文件名乱码问题

5

1、upload.jsp

 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>   
    <title>my file upload</title>
  </head>
  <body>                        
    <s:form action="uploadAction" method="post" enctype="multipart/form-data">
        <s:file name="upload" label="文件上传"></s:file>
        <s:submit value="上传"></s:submit>
    </s:form>
  </body>
</html>

2、struts.xml

 <struts>   
    <!-- <constant name="struts.custom.i18n.resources" value="message"></constant> -->  
    <package name="default" extends="struts-default">
        <action name="uploadAction" class="com.action.UploadAction">
            <result>/success.jsp</result>
        </action>
    </package>
</struts>

3、UploadAction.java

package com.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.logging.Logger;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport{

    private File upload;    //上传的文件
    private String uploadName;//上传的文件名
    public File getUpload() {
        return upload;
    }
    public void setUpload(File upload) {
        this.upload = upload;
    }
    public String getUploadName() {
        return uploadName;
    }
    public void setUploadName(String uploadName) {
        this.uploadName = uploadName;
    }
    public String execute() throws Exception{
        InputStream is=new FileInputStream(getUpload());//获取文件的输入流
        OutputStream os=new FileOutputStream("d:\\upload\\"+getUpload().getName());//指定输出流地址
        byte buffer[] = new byte[1024];
        int count=0;
        while((count=is.read(buffer))>0){
            os.write(buffer,0,count);
        }
        os.close();
        is.close();
        return SUCCESS;
    }
}

4、上传的结果:文件名乱码

图片说明

图片说明

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答 默认 最新

相关推荐