用commons-fileupload实现文件上传
来源:互联网 发布:linux 怎么指定ip登陆 编辑:程序博客网 时间:2024/05/21 06:46
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- final long MAX_SIZE = 3 * 1024 * 1024;
-
- final String[] allowedExt = new String[] { "jpg", "jpeg", "gif","png" };
-
- response.setContentType("text/html");
-
-
-
-
- DiskFileItemFactory dfif = new DiskFileItemFactory();
- dfif.setSizeThreshold(4096);
- dfif.setRepository(new File(request.getRealPath("/")
- + "ImagesUploadTemp"));
-
-
- ServletFileUpload sfu = new ServletFileUpload(dfif);
-
- sfu.setSizeMax(MAX_SIZE);
-
- PrintWriter out = response.getWriter();
-
- List fileList = null;
- try {
- fileList = sfu.parseRequest(request);
- } catch (FileUploadException e) {
- if (e instanceof SizeLimitExceededException) {
- out.print("<script>alert('文件尺寸超过规定大小:" + MAX_SIZE
- + "字节');history.back();</script>");
- return;
- }
- e.printStackTrace();
- }
-
- if (fileList == null || fileList.size() == 0) {
- out.print("<script>alert('请选择上传文件!');history.back();</script>");
- return;
- }
- HashMap<String, String> paramMap = new HashMap<String, String>();
-
- Iterator fileItr = fileList.iterator();
-
- FileItem fileUp= null;
- String path = null;
- while (fileItr.hasNext()) {
- FileItem fileItem = null;
- long size = 0;
-
- fileItem = (FileItem) fileItr.next();
-
- if (fileItem == null || fileItem.isFormField()) {
- String formname=fileItem.getFieldName();
- String formcontent=fileItem.getString();
- formname=new String(formname.getBytes(),"GBK");
- formcontent=new String(formcontent.getBytes(),"GBK");
- paramMap.put(formname, formcontent);
- }
- else{
-
- fileUp= fileItem;
-
- path = fileItem.getName();
-
- size = fileItem.getSize();
- if ("".equals(path) || size == 0) {
- out.print("<script>alert('请选择上传文件!');history.back();</script>");
- return;
- }
-
-
-
- }
-
- }
-
- String t_name = path.substring(path.lastIndexOf("\\") + 1);
-
- String t_ext = t_name.substring(t_name.lastIndexOf(".") + 1);
-
- int allowFlag = 0;
- int allowedExtCount = allowedExt.length;
- for (; allowFlag < allowedExtCount; allowFlag++) {
- if (allowedExt[allowFlag].equals(t_ext))
- break;
- }
- if (allowFlag == allowedExtCount) {
- StringBuffer sb = new StringBuffer();
- for (allowFlag = 0; allowFlag < allowedExtCount; allowFlag++)
- sb.append("*." + allowedExt[allowFlag]);
- out.println("<script>alert('请上传以下类型的文件" + sb.toString()
- + "');history.back();</script>");
- return;
- }
- long now = System.currentTimeMillis();
-
- String prefix = String.valueOf(now);
-
- String u_name = request.getRealPath("/") + "ImagesUploaded/"
- + prefix + "." + t_ext;
-
- String file_url = request.getContextPath() + "/"
- + "ImagesUploaded/" + prefix + "." + t_ext;
- try {
-
- fileUp.write(new File(u_name));
- out.println("<script type='text/javascript'>parent.KE.plugin[\"image\"].insert('" +paramMap.get("id")
- + "', '" + file_url + "','" +paramMap.get("imgWidth") + "','"
- +paramMap.get("imgHeight") + "','" +paramMap.get("imgBorder") + "','" +paramMap.get("imgTitle")
- + "')</script>");
-
- } catch (Exception e) {
- e.printStackTrace();
- }