package action; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.List; import java.util.UUID; import org.apache.commons.io.FileUtils; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class UploadFile extends ActionSupport { private List<File> file; private List<String> fileFileName; private List<String> fileContentType; private List<String> dirpath; public List<String> getDirpath() { return dirpath; } public void setDirpath(List<String> dirpath) { this.dirpath = dirpath; } public List<File> getFile() { return file; } public void setFile(List<File> file) { this.file = file; } public List<String> getFileFileName() { return fileFileName; } public void setFileFileName(List<String> fileFileName) { this.fileFileName = fileFileName; } public List<String> getFileContentType() { return fileContentType; } public void setFileContentType(List<String> fileContentType) { this.fileContentType = fileContentType; } @Override public String execute() throws Exception { // TODO Auto-generated method stub dirpath =new ArrayList(); List list =new ArrayList(); String path = ServletActionContext.getRequest().getRealPath("/upload/"); for(int i=0;i<file.size();i++) { dirpath.add(path+fileFileName.get(i)); //文件上传 String randname = this.FileName(fileFileName.get(i)); list.add(randname); FileUtils.copyFile(file.get(i), new File(path,randname)); System.out.println(fileContentType.get(i)); } // for(int i=0;i<file.size();i++) // { // dirpath.add(path+fileFileName.get(i)); // //文件上传通过stream流 // InputStream is = new FileInputStream(file.get(i)); // OutputStream os = new FileOutputStream(new File(path,this.FileName(fileFileName.get(i)))); // byte[] buffer = new byte[400]; // while(is.read(buffer)>0) // { // os.write(buffer, 0, buffer.length); // } // } ActionContext.getContext().put("list", list); ActionContext.getContext().put("type", fileContentType); return SUCCESS; } public String FileName(String fname) { String ext = fname.substring(fname.lastIndexOf(".")); String uuid = UUID.randomUUID().toString(); return uuid+ext; } }