struts2多文件上传

jsp页面

<form action="${pageContext.request.contextPath}/user/fileUpload.action"
//文件上传这个不能没有
enctype="multipart/form-data"
method="post">
            文件1:<input type="file" name="upload"><br/>
            文件2:<input type="file" name="upload"><br/>
            文件3:<input type="file" name="upload"><br/>
                <input type="submit" value="上传" />
   </form>      


struts.xml

//设置上传文件大小,默认是2M 不设置只能上传2M大了就会报错
<constant name="struts.multipart.maxSize" value="10485760"/>


action

    //要有get()、set()方法
    private List<File> upload;
    private List<String> uploadFileName;
    private List<String> uploadContentType;

        String root=Common.PICTRUE_ROOT_DIR;
    File dir=FileUtil.createDir(root);
    for(int i=0;i<upload.size();i++){
    System.out.println(i);
InputStream is=new FileInputStream(upload.get(i));
String fileName=this.getUploadFileName().get(i);
Long time=System.currentTimeMillis();
String saveFileName=time.toString()+fileName.substring(fileName.lastIndexOf("."));
File uploadFile=new File(dir,saveFileName);
String path=FileUtil.getSubPath(Common.PICTRUE_ROOT_DIR, uploadFile);
String filepath =path.substring(0,path.lastIndexOf("\\"))+"\\" + fileName;
map.put(i,filepath);
OutputStream os=new FileOutputStream(uploadFile);
byte[] buffer=new byte[1024*1024];
int length;
while((length = is.read(buffer))>0){
os.write(buffer,0,length);
}
is.close();
os.close();
}

你可能感兴趣的:(jsp,struts)