Java Code Examples for org.apache.commons.fileupload.servlet.ServletFileUpload#setSizeMax()
The following examples show how to use
org.apache.commons.fileupload.servlet.ServletFileUpload#setSizeMax() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FilesApiServiceImpl.java From multiapps-controller with Apache License 2.0 | 5 votes |
private FileItemIterator createFileIterator(HttpServletRequest request) throws IOException, FileUploadException { long maxUploadSize = new Configuration().getMaxUploadSize(); ServletFileUpload upload = getFileUploadServlet(); upload.setSizeMax(maxUploadSize); try { return upload.getItemIterator(request); } catch (SizeLimitExceededException ex) { throw new SLException(MessageFormat.format(Messages.MAX_UPLOAD_SIZE_EXCEEDED, maxUploadSize)); } }
Example 2
Source File: Uploader.java From jeewx with Apache License 2.0 | 5 votes |
private void parseParams() { DiskFileItemFactory dff = new DiskFileItemFactory(); try { ServletFileUpload sfu = new ServletFileUpload(dff); sfu.setSizeMax(this.maxSize); sfu.setHeaderEncoding(Uploader.ENCODEING); FileItemIterator fii = sfu.getItemIterator(this.request); while (fii.hasNext()) { FileItemStream item = fii.next(); // 普通参数存储 if (item.isFormField()) { this.params.put(item.getFieldName(), this.getParameterValue(item.openStream())); } else { // 只保留一个 if (this.inputStream == null) { this.inputStream = item.openStream(); this.originalName = item.getName(); return; } } } } catch (Exception e) { this.state = this.errorInfo.get("UNKNOWN"); } }
Example 3
Source File: MultipartUtil.java From lutece-core with BSD 3-Clause "New" or "Revised" License | 5 votes |
/** * Convert a HTTP request to a {@link MultipartHttpServletRequest} * * @param nSizeThreshold * the size threshold * @param nRequestSizeMax * the request size max * @param bActivateNormalizeFileName * true if the file name must be normalized, false otherwise * @param request * the HTTP request * @return a {@link MultipartHttpServletRequest}, null if the request does not have a multipart content * @throws SizeLimitExceededException * exception if the file size is too big * @throws FileUploadException * exception if an unknown error has occurred */ public static MultipartHttpServletRequest convert( int nSizeThreshold, long nRequestSizeMax, boolean bActivateNormalizeFileName, HttpServletRequest request ) throws FileUploadException { if ( !isMultipart( request ) ) { return null; } // Create a factory for disk-based file items DiskFileItemFactory factory = new DiskFileItemFactory( ); // Set factory constraints factory.setSizeThreshold( nSizeThreshold ); // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload( factory ); // Set overall request size constraint upload.setSizeMax( nRequestSizeMax ); // get encoding to be used String strEncoding = Optional.ofNullable( request.getCharacterEncoding( ) ).orElse( EncodingService.getEncoding( ) ); Map<String, List<FileItem>> mapFiles = new HashMap<>( ); Map<String, String [ ]> mapParameters = new HashMap<>( ); List<FileItem> listItems = upload.parseRequest( request ); // Process the uploaded items for ( FileItem item : listItems ) { processItem( item, strEncoding, bActivateNormalizeFileName, mapFiles, mapParameters ); } return new MultipartHttpServletRequest( request, mapFiles, mapParameters ); }
Example 4
Source File: FessMultipartRequestHandler.java From fess with Apache License 2.0 | 5 votes |
protected ServletFileUpload createServletFileUpload(final HttpServletRequest request) { final DiskFileItemFactory fileItemFactory = createDiskFileItemFactory(); final ServletFileUpload upload = newServletFileUpload(fileItemFactory); upload.setHeaderEncoding(request.getCharacterEncoding()); upload.setSizeMax(getSizeMax()); return upload; }
Example 5
Source File: UploadServlet.java From tutorials with MIT License | 5 votes |
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { if (ServletFileUpload.isMultipartContent(request)) { DiskFileItemFactory factory = new DiskFileItemFactory(); factory.setSizeThreshold(MEMORY_THRESHOLD); factory.setRepository(new File(System.getProperty("java.io.tmpdir"))); ServletFileUpload upload = new ServletFileUpload(factory); upload.setFileSizeMax(MAX_FILE_SIZE); upload.setSizeMax(MAX_REQUEST_SIZE); String uploadPath = getServletContext().getRealPath("") + File.separator + UPLOAD_DIRECTORY; File uploadDir = new File(uploadPath); if (!uploadDir.exists()) { uploadDir.mkdir(); } try { List<FileItem> formItems = upload.parseRequest(request); if (formItems != null && formItems.size() > 0) { for (FileItem item : formItems) { if (!item.isFormField()) { String fileName = new File(item.getName()).getName(); String filePath = uploadPath + File.separator + fileName; File storeFile = new File(filePath); item.write(storeFile); request.setAttribute("message", "File " + fileName + " has uploaded successfully!"); } } } } catch (Exception ex) { request.setAttribute("message", "There was an error: " + ex.getMessage()); } getServletContext().getRequestDispatcher("/result.jsp").forward(request, response); } }
Example 6
Source File: Uploader.java From jeecg with Apache License 2.0 | 5 votes |
private void parseParams() { DiskFileItemFactory dff = new DiskFileItemFactory(); try { ServletFileUpload sfu = new ServletFileUpload(dff); sfu.setSizeMax(this.maxSize); sfu.setHeaderEncoding(Uploader.ENCODEING); FileItemIterator fii = sfu.getItemIterator(this.request); while (fii.hasNext()) { FileItemStream item = fii.next(); // 普通参数存储 if (item.isFormField()) { this.params.put(item.getFieldName(), this.getParameterValue(item.openStream())); } else { // 只保留一个 if (this.inputStream == null) { this.inputStream = item.openStream(); this.originalName = item.getName(); return; } } } } catch (Exception e) { this.state = this.errorInfo.get("UNKNOWN"); } }
Example 7
Source File: UploadFileController.java From erp-framework with MIT License | 4 votes |
@RequestMapping("/uploadImg") @ResponseBody public ResultBean uploadImg(Long classesId, HttpServletRequest request) { List<Map<String, Object>> resultPath = new ArrayList<Map<String, Object>>(); try { // Create a factory for disk-based file items DiskFileItemFactory factory = new DiskFileItemFactory(); // Set factory constraints // 设置缓冲区大小,这里是4kb factory.setSizeThreshold(4096); // 设置缓冲区目录 factory.setRepository(null); // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); // Set overall request size constraint // 设置最大文件尺寸,这里是4MB upload.setSizeMax(4194304); // 得到所有的文件 List<FileItem> items = upload.parseRequest(request); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); // 文件夹 String uploadDir = REAL_PATH + File.separator + sdf.format(new Date()); //虚拟路径 String uploadVirPath = TEMP_PATH + File.separator + sdf.format(new Date()); Iterator<FileItem> i = items.iterator(); while (i.hasNext()) { FileItem fi = i.next(); String fileName = fi.getName(); if (fileName != null) { // 解决文件名乱码问题 File fullFile = new File(new String(fi.getName().getBytes(), "utf-8")); File savedFile = new File(uploadDir, fullFile.getName()); if (!savedFile.getParentFile().exists()) { savedFile.getParentFile().mkdir(); } if (!savedFile.exists()) { savedFile.createNewFile(); } Map<String, Object> map = new HashMap<String, Object>(); map.put("url", uploadVirPath + "/" + fullFile.getName()); map.put("fileSize", (fi.getSize())); fi.write(savedFile); resultPath.add(map); } } } catch (Exception e) { e.printStackTrace(); } return new ResultBean(resultPath); }
Example 8
Source File: UploadUtils.java From Shop-for-JavaWeb with MIT License | 4 votes |
/** * 处理上传内容 * * @param request * @param maxSize * @return */ // @SuppressWarnings("unchecked") private Map<String, Object> initFields(HttpServletRequest request) { // 存储表单字段和非表单字段 Map<String, Object> map = new HashMap<String, Object>(); // 第一步:判断request boolean isMultipart = ServletFileUpload.isMultipartContent(request); // 第二步:解析request if (isMultipart) { // Create a factory for disk-based file items DiskFileItemFactory factory = new DiskFileItemFactory(); // 阀值,超过这个值才会写到临时目录,否则在内存中 factory.setSizeThreshold(1024 * 1024 * 10); factory.setRepository(new File(tempPath)); // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); upload.setHeaderEncoding("UTF-8"); // 最大上传限制 upload.setSizeMax(maxSize); /* FileItem */ List<FileItem> items = null; // Parse the request try { items = upload.parseRequest(request); } catch (FileUploadException e) { // TODO Auto-generated catch block e.printStackTrace(); } // 第3步:处理uploaded items if (items != null && items.size() > 0) { Iterator<FileItem> iter = items.iterator(); // 文件域对象 List<FileItem> list = new ArrayList<FileItem>(); // 表单字段 Map<String, String> fields = new HashMap<String, String>(); while (iter.hasNext()) { FileItem item = iter.next(); // 处理所有表单元素和文件域表单元素 if (item.isFormField()) { // 表单元素 String name = item.getFieldName(); String value = item.getString(); fields.put(name, value); } else { // 文件域表单元素 list.add(item); } } map.put(FORM_FIELDS, fields); map.put(FILE_FIELDS, list); } } return map; }
Example 9
Source File: UploadUtils.java From jeewx with Apache License 2.0 | 4 votes |
/** * 处理上传内容 * * @param request * @param maxSize * @return */ @SuppressWarnings("unchecked") private Map<String, Object> initFields(HttpServletRequest request) { // 存储表单字段和非表单字段 Map<String, Object> map = new HashMap<String, Object>(); // 第一步:判断request boolean isMultipart = ServletFileUpload.isMultipartContent(request); // 第二步:解析request if (isMultipart) { // Create a factory for disk-based file items DiskFileItemFactory factory = new DiskFileItemFactory(); // 阀值,超过这个值才会写到临时目录,否则在内存中 factory.setSizeThreshold(1024 * 1024 * 10); factory.setRepository(new File(tempPath)); // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); upload.setHeaderEncoding("UTF-8"); // 最大上传限制 upload.setSizeMax(maxSize); /* FileItem */ List<FileItem> items = null; // Parse the request try { items = upload.parseRequest(request); } catch (FileUploadException e) { // TODO Auto-generated catch block e.printStackTrace(); } // 第3步:处理uploaded items if (items != null && items.size() > 0) { Iterator<FileItem> iter = items.iterator(); // 文件域对象 List<FileItem> list = new ArrayList<FileItem>(); // 表单字段 Map<String, String> fields = new HashMap<String, String>(); while (iter.hasNext()) { FileItem item = iter.next(); // 处理所有表单元素和文件域表单元素 if (item.isFormField()) { // 表单元素 String name = item.getFieldName(); String value = item.getString(); fields.put(name, value); } else { // 文件域表单元素 list.add(item); } } map.put(FORM_FIELDS, fields); map.put(FILE_FIELDS, list); } } return map; }
Example 10
Source File: FileUploadUtils.java From TinyMooc with Apache License 2.0 | 4 votes |
public static final HashMap UploadFile(HttpServletRequest request, HttpServletResponse response, String filepaths) throws Exception { HashMap params = new HashMap(); String pathType = "images"; if (!(filepaths.equals(""))) pathType = filepaths; String readPath = ""; String contextPath = ""; if ((readPath == null) || (readPath.equals(""))) readPath = request.getSession().getServletContext().getRealPath(""); if ((contextPath == null) || (contextPath.equals(""))) contextPath = request.getContextPath(); String savePhotoPath = mkdirFile(readPath, pathType, true); if (ServletFileUpload.isMultipartContent(request)) { DiskFileItemFactory dff = new DiskFileItemFactory(); dff.setSizeThreshold(1024000); ServletFileUpload sfu = new ServletFileUpload(dff); List fileItems = sfu.parseRequest(request); sfu.setFileSizeMax(5000000L); sfu.setSizeMax(10000000L); sfu.setHeaderEncoding("UTF-8"); Iterator iter = fileItems.iterator(); while (iter.hasNext()) { FileItem item = (FileItem) iter.next(); if (item.isFormField()) { String name = item.getFieldName(); String value = new String(item.getString().getBytes( "ISO8859-1"), "utf-8"); params.put(name, value); } else if ((item.getSize() > 0L) && (item.getName() != null)) { File fullFile = new File(item.getName()); File fileOnServer = new File(savePhotoPath, fullFile.getName()); item.write(fileOnServer); String fileName2 = fileOnServer.getName(); String fileType = fileName2.substring(fileName2 .lastIndexOf(".")); String filepath = savePhotoPath + "/" + new Date().getTime() + fileType; File newFile = new File(filepath); fileOnServer.renameTo(newFile); String returnString = filepath.substring(readPath .length()); params.put(item.getFieldName(), request.getRequestURL().toString().replace(request.getRequestURI(), "") + contextPath + returnString); } } } return params; }
Example 11
Source File: PostReceiver.java From android-lite-http with Apache License 2.0 | 4 votes |
/** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //String fileDir = "D:\\Downloads"; //这是我的Mac笔记本上的位置,开发者设置为合适自己的文件夹,尤其windows系统。 String fileDir = "/Users/matianyu/downloads/lite-http-v3/"; String contentType = request.getContentType(); System.out.println("_________________ content type: " + contentType); // 接受一般参数 Map<String, String[]> map = request.getParameterMap(); if (map.size() > 0) { System.out.println("_________________ http params - start"); for (Entry<String, String[]> en : map.entrySet()) { System.out.println(en.getKey() + " : " + Arrays.toString(en.getValue())); } System.out.println("_________________ http params - over"); } response.setContentType("text/plain"); response.setCharacterEncoding("UTF-8"); // 接受文件和流 PrintWriter writer = response.getWriter(); writer.println("contentType:" + contentType); if (contentType != null) { if (contentType.startsWith("multipart")) { //向客户端发送响应正文 try { //创建一个基于硬盘的FileItem工厂 DiskFileItemFactory factory = new DiskFileItemFactory(); //设置向硬盘写数据时所用的缓冲区的大小,此处为4K factory.setSizeThreshold(4 * 1024); //设置临时目录 factory.setRepository(new File(fileDir)); //创建一个文件上传处理器 ServletFileUpload upload = new ServletFileUpload(factory); //设置允许上传的文件的最大尺寸,此处为100M upload.setSizeMax(100 * 1024 * 1024); Map<String, List<FileItem>> itemMap = upload.parseParameterMap(request); for (List<FileItem> items : itemMap.values()) { Iterator iter = items.iterator(); while (iter.hasNext()) { FileItem item = (FileItem) iter.next(); if (item.isFormField()) { processFormField(item, writer); //处理普通的表单域 } else { processUploadedFile(fileDir, item, writer); //处理上传文件 } } } } catch (Exception ex) { ex.printStackTrace(); } } else if (contentType.contains("text") || contentType.contains("json") || contentType.contains("application/x-www-form-urlencoded") || contentType.contains("xml")) { processString(request); } else { processEntity(fileDir, request); } } else { processString(request); } System.out.println("doPost over"); writer.print("upload over. "); }
Example 12
Source File: UploadUtils.java From jeecg with Apache License 2.0 | 4 votes |
/** * 处理上传内容 * * @param request * @param maxSize * @return */ @SuppressWarnings("unchecked") private Map<String, Object> initFields(HttpServletRequest request) { // 存储表单字段和非表单字段 Map<String, Object> map = new HashMap<String, Object>(); // 第一步:判断request boolean isMultipart = ServletFileUpload.isMultipartContent(request); // 第二步:解析request if (isMultipart) { // Create a factory for disk-based file items DiskFileItemFactory factory = new DiskFileItemFactory(); // 阀值,超过这个值才会写到临时目录,否则在内存中 factory.setSizeThreshold(1024 * 1024 * 10); factory.setRepository(new File(tempPath)); // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); upload.setHeaderEncoding("UTF-8"); // 最大上传限制 upload.setSizeMax(maxSize); /* FileItem */ List<FileItem> items = null; // Parse the request try { items = upload.parseRequest(request); } catch (FileUploadException e) { // TODO Auto-generated catch block e.printStackTrace(); } // 第3步:处理uploaded items if (items != null && items.size() > 0) { Iterator<FileItem> iter = items.iterator(); // 文件域对象 List<FileItem> list = new ArrayList<FileItem>(); // 表单字段 Map<String, String> fields = new HashMap<String, String>(); while (iter.hasNext()) { FileItem item = iter.next(); // 处理所有表单元素和文件域表单元素 if (item.isFormField()) { // 表单元素 String name = item.getFieldName(); String value = item.getString(); fields.put(name, value); } else { // 文件域表单元素 list.add(item); } } map.put(FORM_FIELDS, fields); map.put(FILE_FIELDS, list); } } return map; }