补充个简化的能支持多个文件同时上传的例子:
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{
private List file;
private List fileFileName;
private List 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 getFile() {
return file;
}
public void setFile(List file) {
this.file = file;
}
public List getFileFileName() {
return fileFileName;
}
public void setFileFileName(List fileFileName) {
this.fileFileName = fileFileName;
}
public List getFileContentType() {
return fileContentType;
}
public void setFileContentType(List fileContentType) {
this.fileContentType = fileContentType;
}
}
1.首先是上传页面
FileUpload.jsp
pageEncoding="utf-8"%>
html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
Insert title hereGO
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 imageFileName;//新文件名称由当前时间和文件后缀名组成
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(){
imageFileName = new Date().getTime() + getExtention(fileName);
//File imageFile = new File(ServletActionContext.getServletContext().getRealPath(" /UploadImages" + "/" +imageFileName));
String filePath = ServletActionContext.getServletContext().getRealPath("UploadImages" + "/" +imageFileName);
//这里获取的是服务器的路径,可以打出来看下
System.out.println("系统路径:"+filePath);
File imageFile = new File(filePath);
copy(myFile,imageFile);
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 imageFileName;
}
public void setImageFileName(String imageFileName) {
this.imageFileName = imageFileName;
}
public String getCaption() {
return caption;
}
public void setCaption(String caption) {
this.caption = caption;
}
}
3.ShowUpload.jsp
pageEncoding="utf-8"%>
html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
Insert title here