import java.io.File;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadBase;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class SlideUploadAction extends Action {
@SuppressWarnings("unchecked")
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String newName = request.getParameter("newName");
String uploadPath = this.getServlet().getServletContext().getRealPath(
"/upload/slide/"); // 上传文件的目录
// getRealPath 返回值在不同服务器上有的带反斜杠,有的不带
if (!uploadPath.endsWith(File.separator)) {
uploadPath += File.separator;
}
String tempPath = uploadPath + "temp/"; // 临时文件目录
// 检查文件上传目录和临时目录是否存在,若不存在则创建目录。
File uploadFile = new File(uploadPath);
if (!uploadFile.exists()) {
uploadFile.mkdirs();
}
File tempPathFile = new File(tempPath);
if (!tempPathFile.exists()) {
tempPathFile.mkdirs();
}
PrintWriter writer = response.getWriter();
// OutputStream out = response.getOutputStream();
boolean isError = false;
String error = "";
String msg = "";
// Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();
// Set factory constraints
factory.setSizeThreshold(4096); // 设置缓冲区大小,这里是4kb
factory.setRepository(tempPathFile);// 设置缓冲区目录
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// Set overall request size constraint
upload.setSizeMax(2 * 1024 * 1024); // 设置最大文件尺寸,这里是4MB
try {
List items = upload.parseRequest(request);// 得到所有的文件
System.out.println("----items:" + items.size());
Iterator i = items.iterator();
while (i.hasNext()) {
FileItem fi = (FileItem) i.next();
System.out.println("----fileSize:" + fi.getSize());
String fileName = fi.getName();
if (fileName.isEmpty()) {
isError = true;
error = "没有文件要上传!";
} else if (!fileName.endsWith(".jpg")) {
isError = true;
error = "只能上传jpg格式的文件!";
} else if (fi.getSize() > 2 * 1024 * 1024) {
isError = true;
error = "文件不能超过2M!";
throw new Exception("文件超过大小");
}
if (!isError) {
File fullFile = new File(newName);
File savedFile = new File(uploadPath, fullFile.getName());
fi.write(savedFile);
}
}
System.out.print("upload succeed");
msg = "上传完成!";
} catch (FileUploadBase.SizeLimitExceededException e) {
error = "文件超出大小限制!";
} catch (FileUploadBase.IOFileUploadException e) {
error = "传输错误,可能磁盘空间不足!";
} catch (FileUploadBase.InvalidContentTypeException e) {
error = "无效的请求类型,enctype != 'multipart/form-data'";
} catch (Exception e) {
// 可以跳转出错页面
if (error.isEmpty()) {
error = "出现未知错误,上传失败";
}
e.printStackTrace();
}
writer.println("{");
writer.println("error: '" + error + "',");
writer.println("msg: '" + msg + "'");
writer.println("}");
writer.flush();
writer.close();
return null;
}
}
阅读(1243) | 评论(1) | 转发(0) |