补充个简化的能支持多个文件同时上传的例子:
<body> <center> <font color="red"><s:fielderror/></font> <s:form action="perLogin1" method="post" enctype="multipart/form-data"> <s:file name="file" label="文件1"></s:file> <s:file name="file" label="文件2"></s:file> <s:file name="file" label="文件3"></s:file> <s:file name="file" label="文件4"></s:file> <s:submit label="上传"/> </s:form> </center> </body> package com.usr.hb.PerLogin.action; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.List; import org.apache.struts2.ServletActionContext; import com.framework.struts2.action.AbstractAction; import com.usr.hb.PerLogin.business.ebi.PerLoginEbi; import com.usr.hb.PerLogin.model.PerLoginModel; import com.usr.hb.PerLogin.model.PerLoginQueryModel; public class PerLogin1Action extends AbstractAction<PerLoginModel, PerLoginQueryModel, PerLoginEbi>{ private List<File> file; private List<String> fileFileName; private List<String> fileContentType; public String execute() throws IOException{ //循环上传的文件 for(int i = 0 ; i < file.size() ; i ++){ InputStream is = new FileInputStream(file.get(i)); System.out.println(fileFileName.get(i)+"---------"+fileContentType.get(i)); //得到图片保存的位置 File destFile = new File("D:/HBGW/"+fileFileName.get(i)); //把图片写入到上面设置的路径里 OutputStream os = new FileOutputStream(destFile); byte[] buffer = new byte[400]; int length = 0 ; while((length = is.read(buffer))>0){ os.write(buffer, 0, length); } is.close(); os.close(); } return SUCCESS; } 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; } }
1.首先是上传页面
FileUpload.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<s:form action="ul" method="POST" enctype="multipart/form-data">
<s:file name="myFile" label="Image File">
<s:textfield name="caption" label="Caption"></s:textfield>
<s:submit>GO</s:submit>
</s:file>
</s:form>
</body>
</html>
2.通过struts配置关联Action,这里就不多介绍了。
package com.zc.text.zct.action;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class FileUpploadAction extends ActionSupport{
private static final long serialVersionUID = -622186839813885520L;
private static final int BUFFER_SIZE =16 * 1024;
private File myFile;//是对上传文件名称+路径进行编译后的串
private String contentType;//文件类型
private String fileName;//页面上传文件的名称
private String p_w_picpathFileName;//新文件名称由当前时间和文件后缀名组成
private String caption;//标题说明
/**
*
* TODO 通过流复制文件
* @author :张驰
* @param src
* @param dst
*/
public static void copy(File src,File dst){
InputStream in = null;
OutputStream out = null;
try{
in = new BufferedInputStream(new FileInputStream(src),BUFFER_SIZE);
out = new BufferedOutputStream(new FileOutputStream(dst),BUFFER_SIZE);
try{
byte [] buffer = new byte [BUFFER_SIZE];
while(in.read(buffer)>0){
out.write(buffer);
}
out.flush();
}finally{
if(null != in){
in.close();
}
if(null != out){
out.close();
}
}
}catch (Exception e) {
// TODO: handle exception
}
}
/**
*
* TODO 处理文件的名字
* @author :张驰
* @param fileName
* @return
*/
public static String getExtention(String fileName){
int pos = fileName.lastIndexOf(".");
return fileName.substring(pos);
}
/**
*
*/
public String execute(){
p_w_picpathFileName = new Date().getTime() + getExtention(fileName);
//File p_w_picpathFile = new File(ServletActionContext.getServletContext().getRealPath(" /UploadImages" + "/" +p_w_picpathFileName));
String filePath = ServletActionContext.getServletContext().getRealPath("UploadImages" + "/" +p_w_picpathFileName);
//这里获取的是服务器的路径,可以打出来看下
System.out.println("系统路径:"+filePath);
File p_w_picpathFile = new File(filePath);
copy(myFile,p_w_picpathFile);
return "upload";
}
public void setMyFileContentType(String contentType) {
this .contentType = contentType;
}
public void setMyFileFileName(String fileName) {
this .fileName = fileName;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public File getMyFile() {
return myFile;
}
public void setMyFile(File myFile) {
this.myFile = myFile;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public String getImageFileName() {
return p_w_picpathFileName;
}
public void setImageFileName(String p_w_picpathFileName) {
this.p_w_picpathFileName = p_w_picpathFileName;
}
public String getCaption() {
return caption;
}
public void setCaption(String caption) {
this.caption = caption;
}
}
转载于:https://blog.51cto.com/zhangchi/1031406