public ActionForward upLoad(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws FileNotFoundException, IOException {
FileForm fileForm=(FileForm)form;
List<FileForm.myFile> files=fileForm.getImportFileList();
for(int i =0;i<files.size();i++){
FileForm.myFile uploadFile = files.get(i);
FormFile file = uploadFile.getFile();
if(file==null||file.getFileName()==null||"".equals(file.getFileName())){
System.out.println("file is null");
continue;//这里判断JSP页面中的input框是否有值
// return new ActionForward(request.getHeader("Referer"),true);
}
else{
String fileName = file.getFileName();
int fileSize = file.getFileSize();
byte[] fileDate = file.getFileData();
//下面这句得到服务器上的上传目录+文件名
String path=this.getServlet().getServletContext().getRealPath("/")+UPLOAD_FOLDER+fileName;
UploadUtil.saveFile(path,fileDate,fileSize,File_DEST);//调用uplodautil中的方法上传文件
file.destroy();
}
}
return mapping.findForward("success");
}
formbean:
public class FileForm extends ActionForm {
/*
* Generated fields
*/
public class myFile {
private FormFile file;
public FormFile getFile() {
return file;
}
public void setFile(FormFile file) {
this.file = file;
}
}
public static final String ERROR_PROPERTY_MAX_LENGTH_EXCEEDED = "org.apache.struts.webapp.upload.MaxLengthExceeded";
protected List<myFile> importFile;
public FileForm()
{
importFile=new ArrayList<myFile>();
importFile.add(0,new myFile());//初始化页面只有一个input
}
/*
* Generated Methods
*/
/**
* Method validate
*
* @param mapping
* @param request
* @return ActionErrors
*/
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors=new ActionErrors();
Boolean maxLengthExceeded = (Boolean) request.getAttribute(
MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED);
if ((maxLengthExceeded != null) && (maxLengthExceeded.booleanValue()))
{
errors.add("文件", new ActionMessage("too big"));
}
return errors;
/*if (null == importFile.getFileName() || "".equals(theFile.getFileName())) {
errors.add("theFileError", new ActionMessage("errors.upload.file"));
}*/
}
/**
* Method reset
*
* @param mapping
* @param request
*/
public void reset(ActionMapping mapping, HttpServletRequest request) {
// TODO Auto-generated method stub
}
public List<myFile> getImportFileList() {
return importFile;
}
public void setImportFileList(List<myFile> importFile) {
this.importFile = importFile;
}
public myFile getImportFile(int index) {
int size = importFile.size();
if (index > size-1) {
for (int i=0;i<index-size+1;i++){
importFile.add(new myFile());
}
}
return importFile.get(index);
}
public void setImportFile(int index,FormFile file) {
myFile element= new myFile();
element.setFile(file);
this.importFile .add(index, element);
}
}
JSP页面:
<body>
<html:form action="/file.do?method=upLoad" method="post" enctype="multipart/form-data">
<%--<logic:iterate id="importFile" name="fileForm" property="importFileList" indexId="index">
<html:file property="file" name="importFile" indexed="true"/><html:errors property="theFile"/><br/>
</logic:iterate>--%>
<input type="file" name="importFile[0].file"/><br>
<input type="file" name="importFile[1].file"/><br>
<input type="file" name="importFile[2].file"/><br>
<input type="file" name="importFile[3].file"/><br>
<input type="file" name="importFile[4].file"/><br>
<input type="file" name="importFile[5].file"/><br>
<html:submit/><html:cancel/>
</html:form>
</body>
uploadUtil 类:
package com.yourcompany.struts.action;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class UploadUtil {
private static final String DATABASE_DEST = "database";
private static final String FILE_DEST = "file";
private static final int MAX_SIZE = 1024 * 1024;
private static final String[] TYPES = { ".jpg", ".gif", ".zip", ".rar" };
public static void saveFile(String fileName, byte[] fileData, int size,
String dest) throws FileNotFoundException, IOException {
if (!checkSize(size)) {
throw new IOException(size + " is too large !");
}
if (!checkType(fileName)) {
throw new IOException("Unvaildate type !");
}
if (dest.equals(DATABASE_DEST)) {
saveToDb(fileName, fileData);
}
if (dest.equals(FILE_DEST)) {
saveToFile(fileName, fileData);
}
}
private static void saveToDb(String fileName, byte[] fileData) {
}
private static void saveToFile(String fileName, byte[] fileData)
throws FileNotFoundException, IOException {
OutputStream o = new FileOutputStream(fileName);
o.write(fileData);
o.close();
}
public static void delFile(String fileName, String dest)
throws NullPointerException, SecurityException {
if (dest.equals(DATABASE_DEST)) {
delFromDb(fileName);
}
if (dest.equals(FILE_DEST)) {
delFromFile(fileName);
}
}
private static void delFromDb(String fileName) {
}
private static void delFromFile(String fileName)
throws NullPointerException, SecurityException {
File file = new File(fileName);
if (file.exists())
file.delete();
}
private static boolean checkSize(int size) {
if (size > MAX_SIZE)
return false;
return true;
}
private static boolean checkType(String fileName) {
for (int i = 0; i < TYPES.length; i++) {
if (fileName.toLowerCase().endsWith(TYPES[i])) {
return true;
}
}
return false;
}
}