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;
}
}
分享到:
相关推荐
struts上传struts上传 struts上传struts上传
struts上传文件(图片/附件),希望对初学struts的同学们有帮助。如果有事么不足,请大家多指教。
struts 上传 和 smartupload上传
实现struts同时上传多个文件!
Struts上传,已经处理好乱码问题Struts上传,已经处理好乱码问题
这是个文件上传与下载的文件,按步骤操作,准能成功!祝你成功!
STRUTS框架的上传DEMO,解决上传后文件名中文乱码问题! 软件为MYECLIPS直接导出,使用方法:解压后直接导入到MYECLIPS就可使用!
基于struts上传头像功能,采用showModalDialog进行窗口弹出上传,关闭窗口实时更新头像,有需要的请猛击下载
用struts上传多个文件的方法,建立上传用的JSP文件配置struts-config.xml文件
Struts上传图片,本地或者项目中,希望带给大家一点帮助!
struts 上传 组件 struts 上传 组件 具体操作
struts上传.rar
struts上传和下载源码
struts上传下载实例包含了完成的代码以及jsp
这是我在教学中的struts上传课堂演示案例,包括单个上传和批量上传,并附上教学PPT。希望对初学者有所帮组
struts上传下载源代码,用java实现文件的上传与下载
struts上传与显示图片例子参考 struts上传与显示图片例子参考 struts上传与显示图片例子参考