import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
public class FileUpload extends HttpServlet {
private static final long serialVersionUID = 1L;
private static Log logger = LogFactory.getLog(FileUpload.class);
@Override
protected void doGet(HttpServletRequest req,HttpServletResponse resp)
throws ServletException,IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req,IOException {
// 类型 1事件上传文件 2apk
String t = req.getParameter("t") == null ? "1" : req.getParameter("t")
.trim();
String path = "";
JSONObject ob = new JSONObject();
try {
//将请求消息中的每一个项目封装成单独DiskFileItem对象的任务
//当上传的文件项目比较小时保存在内存中,比较大时保存在磁盘零时文件夹中
//建立文件仓库(工厂)
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload servletFileUpload = new ServletFileUpload(factory);
//对上传的文件进行设定
servletFileUpload.setSizeMax(1024 1024 2);// 最大2M数据
servletFileUpload.setFileSizeMax(2 1024 1024);
servletFileUpload.setHeaderEncoding("UTF-8");// 解决文件名乱码的问题
//解析请求正文,获取上传文件,不抛出异常则写入真实路径
//根据请求获取文件列表
List fileItemsList = servletFileUpload.parseRequest(req);
//从文件列表中取出单独的文件对象
for (FileItem item : fileItemsList) {
//判断该文件是否是普通的表单类型,该处是file类型进入判断
if (!item.isFormField()) {
//如果上传的文件大于指定的大小则return
if (item.getSize() > 2 1024 1024) {
return;
}
// System.out.println("上传文件的大小:"+item.getSize());
// System.out.println("上传文件的类型:"+item.getContentType());
// System.out.println("上传文件的名称:"+item.getName());
//上传文件的名称
String fileName = item.getName();
String ent = "";
//内容的类型
if (item.getContentType().equalsIgnoreCase("image/x-png")
|| item.getContentType().equalsIgnoreCase(
"image/png")) {
ent = ".png";
} else if (item.getContentType().equalsIgnoreCase(
"image/gif")) {
ent = ".gif";
} else if (item.getContentType().equalsIgnoreCase(
"image/bmp")) {
ent = ".bmp";
} else if (item.getContentType().equalsIgnoreCase(
"image/pjpeg")
|| item.getContentType().equalsIgnoreCase(
"image/jpeg")) {
ent = ".jpg";
}
//获取文件的是那种格式
if (fileName.lastIndexOf(".") != -1) {
ent = fileName.substring(fileName.lastIndexOf("."));
}
fileName = "ev_" + System.currentTimeMillis() + ent;
// 定义文件路径,根据你的文件夹结构,可能需要做修改
if (t.equals("1")) {
path = "upload/ev/" + fileName;
} else {
path = "upload/pk/" + fileName;
}
// 保存文件到服务器上
File file = new File(req.getSession().getServletContext()
.getRealPath(path));
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
item.write(file);
// logger.info(path);
// break;
ob.accumulate("url",path);
}
}
resp.setContentType("text/html; charset=UTF-8");
resp.getWriter().write(ob.toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
// 响应客户端
// resp.setContentType("text/html; charset=UTF-8");
// resp.getWriter().write(ob.toString());
}
}
}