public class UploadFileUtil extends HttpServlet { private static final long serialVersionUID = 1L; File tmpDir = null;// 初始化上传文件的临时存放目录 File saveDir = null;// 初始化上传文件后的保存目录 public UploadFileUtil() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try{ if(ServletFileUpload.isMultipartContent(request)){ response.setCharacterEncoding("utf-8");//务必,防止返回文件名是乱码 DiskFileItemFactory dff = new DiskFileItemFactory();//创建该对象 dff.setRepository(tmpDir);//指定上传文件的临时目录 dff.setSizeThreshold(1024000);//指定在内存中缓存数据大小,单位为byte ServletFileUpload sfu = new ServletFileUpload(dff);//创建该对象 sfu.setFileSizeMax(5000000);//指定单个上传文件的最大尺寸 sfu.setSizeMax(10000000);//指定一次上传多个文件的总尺寸 FileItemIterator fii = sfu.getItemIterator(request);//解析request 请求,并返回FileItemIterator集合 while(fii.hasNext()){ FileItemStream fis = fii.next();//从集合中获得一个文件流 if(!fis.isFormField() && fis.getName().length()>0){//过滤掉表单中非文件域 String fileName = fis.getName();//获取文件名 String extName = ""; if (fileName.lastIndexOf(".") >= 0) { extName = fileName.substring(fileName.lastIndexOf(".")); } BufferedInputStream in = new BufferedInputStream(fis.openStream());//获得文件输入流 String uuidName = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();//用UUID生成文件名 BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(saveDir+"https://www.jb51.net/"+uuidName+extName)));//获得文件输出流 Streams.copy(in, out, true);//开始把文件写到你指定的上传文件夹 } } //jquery上传方式返回 response.getWriter().print("upload success");//成功 } }catch(Exception e){ response.getWriter().print("upload fail");//失败 e.printStackTrace(); } } public void init() throws ServletException { super.init(); String serverPath = this.getServletConfig().getServletContext().getRealPath("https://www.jb51.net/");//获取服务器路径 String tmpPath = serverPath+"/tmpUploadsFolder/"; String savePath = serverPath+"/uploadsFolder/"; tmpDir = new File(tmpPath); saveDir = new File(savePath); if(!tmpDir.isDirectory()) tmpDir.mkdir(); if(!saveDir.isDirectory()) saveDir.mkdir(); }}
以上内容是小编给大家介绍的jQuery Uploadify 上传插件出现Http Error 302 错误的解决办法,希望大家喜欢。
您可能感兴趣的文章: