/** 获取所有文本域 */ public static final List<?> getFileItemList(HttpServletRequest request, File saveDir) throws FileUploadException { if (!saveDir.isDirectory()) { saveDir.mkdir(); } List<?> fileItems = null; RequestContext requestContext = new ServletRequestContext(request); if (FileUpload.isMultipartContent(requestContext)) { DiskFileItemFactory factory = new DiskFileItemFactory(); factory.setRepository(saveDir); factory.setSizeThreshold(fileSizeThreshold); ServletFileUpload upload = new ServletFileUpload(factory); fileItems = upload.parseRequest(request); } return fileItems; }
/** 获取所有文本域 */ public static List<?> getFileItemList(HttpServletRequest request, File saveDir) throws FileUploadException { if (!saveDir.isDirectory()) { saveDir.mkdir(); } List<?> fileItems = null; RequestContext requestContext = new ServletRequestContext(request); if (FileUpload.isMultipartContent(requestContext)) { DiskFileItemFactory factory = new DiskFileItemFactory(); factory.setRepository(saveDir); factory.setSizeThreshold(fileSizeThreshold); ServletFileUpload upload = new ServletFileUpload(factory); fileItems = upload.parseRequest(request); } return fileItems; }
/** * Creates a RequestContext needed by Jakarta Commons Upload. * * @param req the request. * @return a new request context. */ protected RequestContext createRequestContext(final HttpServletRequest req) { return new RequestContext() { public String getCharacterEncoding() { return req.getCharacterEncoding(); } public String getContentType() { return req.getContentType(); } public int getContentLength() { return req.getContentLength(); } public InputStream getInputStream() throws IOException { InputStream in = req.getInputStream(); if (in == null) { throw new IOException("Missing content in the request"); } return req.getInputStream(); } }; }
public RequestContext getRequestContext(){ return new RequestContext() { @Override public String getCharacterEncoding() { return charEncoding; } @Override public String getContentType() { return contentType; } @Override public int getContentLength() { return body.length; } @Override public InputStream getInputStream() throws IOException { return getBodyInputStream(); } }; }
/** * Creates a RequestContext needed by Jakarta Commons Upload. * * @param req the HTTP request. * @return a new request context. */ private RequestContext createRequestContext(final HttpServletRequest req) { return new RequestContext() { @Override public String getCharacterEncoding() { return req.getCharacterEncoding(); } @Override public String getContentType() { return req.getContentType(); } @Override @Deprecated public int getContentLength() { return req.getContentLength(); } @Override public InputStream getInputStream() throws IOException { return req.getInputStream(); } }; }
private RequestContext createRequestContext(final HttpServletRequest req) { return new RequestContext() { public String getCharacterEncoding() { return req.getCharacterEncoding(); } public String getContentType() { return req.getContentType(); } public int getContentLength() { return req.getContentLength(); } public InputStream getInputStream() throws IOException { return req.getInputStream(); } }; }
@Override public List<FileItem> parseRequest(RequestContext ctx) throws FileUploadException { List<FileItem> items = super.parseRequest(ctx); String charset = ctx.getCharacterEncoding(); for (FileItem fileItem : items) { if (fileItem instanceof AbstractFileItem) { ((AbstractFileItem) fileItem).setCharset(charset); } } return items; }
private MultipleEntryHashMap getParameterMap(RequestContext request, String charSetEncoding) throws FileUploadException { MultipleEntryHashMap parameterMap = new MultipleEntryHashMap(); List items = parseRequest(request); Iterator iter = items.iterator(); while (iter.hasNext()) { DiskFileItem diskFileItem = (DiskFileItem)iter.next(); boolean isFormField = diskFileItem.isFormField(); Object value; try { if (isFormField) { value = getTextParameter(diskFileItem, charSetEncoding); } else { value = getFileParameter(diskFileItem); } } catch (Exception ex) { throw new FileUploadException(ex.getMessage()); } parameterMap.put(diskFileItem.getFieldName(), value); } return parameterMap; }
private static List parseRequest(RequestContext requestContext) throws FileUploadException { // Create a factory for disk-based file items FileItemFactory factory = new DiskFileItemFactory(); // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); // Parse the request return upload.parseRequest(requestContext); }
@Test public void writeMultipart() throws Exception { MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>(); parts.add("name 1", "value 1"); parts.add("name 2", "value 2+1"); parts.add("name 2", "value 2+2"); parts.add("name 3", null); Resource logo = new ClassPathResource("/org/springframework/http/converter/logo.jpg"); parts.add("logo", logo); // SPR-12108 Resource utf8 = new ClassPathResource("/org/springframework/http/converter/logo.jpg") { @Override public String getFilename() { return "Hall\u00F6le.jpg"; } }; parts.add("utf8", utf8); Source xml = new StreamSource(new StringReader("<root><child/></root>")); HttpHeaders entityHeaders = new HttpHeaders(); entityHeaders.setContentType(MediaType.TEXT_XML); HttpEntity<Source> entity = new HttpEntity<Source>(xml, entityHeaders); parts.add("xml", entity); MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); this.converter.setMultipartCharset(UTF_8); this.converter.write(parts, new MediaType("multipart", "form-data", UTF_8), outputMessage); final MediaType contentType = outputMessage.getHeaders().getContentType(); assertNotNull("No boundary found", contentType.getParameter("boundary")); // see if Commons FileUpload can read what we wrote FileItemFactory fileItemFactory = new DiskFileItemFactory(); FileUpload fileUpload = new FileUpload(fileItemFactory); RequestContext requestContext = new MockHttpOutputMessageRequestContext(outputMessage); List<FileItem> items = fileUpload.parseRequest(requestContext); assertEquals(6, items.size()); FileItem item = items.get(0); assertTrue(item.isFormField()); assertEquals("name 1", item.getFieldName()); assertEquals("value 1", item.getString()); item = items.get(1); assertTrue(item.isFormField()); assertEquals("name 2", item.getFieldName()); assertEquals("value 2+1", item.getString()); item = items.get(2); assertTrue(item.isFormField()); assertEquals("name 2", item.getFieldName()); assertEquals("value 2+2", item.getString()); item = items.get(3); assertFalse(item.isFormField()); assertEquals("logo", item.getFieldName()); assertEquals("logo.jpg", item.getName()); assertEquals("image/jpeg", item.getContentType()); assertEquals(logo.getFile().length(), item.getSize()); item = items.get(4); assertFalse(item.isFormField()); assertEquals("utf8", item.getFieldName()); assertEquals("Hall\u00F6le.jpg", item.getName()); assertEquals("image/jpeg", item.getContentType()); assertEquals(logo.getFile().length(), item.getSize()); item = items.get(5); assertEquals("xml", item.getFieldName()); assertEquals("text/xml", item.getContentType()); verify(outputMessage.getBody(), never()).close(); }
@Test public void writeMultipartOrder() throws Exception { MyBean myBean = new MyBean(); myBean.setString("foo"); MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>(); parts.add("part1", myBean); HttpHeaders entityHeaders = new HttpHeaders(); entityHeaders.setContentType(MediaType.TEXT_XML); HttpEntity<MyBean> entity = new HttpEntity<MyBean>(myBean, entityHeaders); parts.add("part2", entity); MockHttpOutputMessage outputMessage = new MockHttpOutputMessage(); this.converter.setMultipartCharset(UTF_8); this.converter.write(parts, new MediaType("multipart", "form-data", UTF_8), outputMessage); final MediaType contentType = outputMessage.getHeaders().getContentType(); assertNotNull("No boundary found", contentType.getParameter("boundary")); // see if Commons FileUpload can read what we wrote FileItemFactory fileItemFactory = new DiskFileItemFactory(); FileUpload fileUpload = new FileUpload(fileItemFactory); RequestContext requestContext = new MockHttpOutputMessageRequestContext(outputMessage); List<FileItem> items = fileUpload.parseRequest(requestContext); assertEquals(2, items.size()); FileItem item = items.get(0); assertTrue(item.isFormField()); assertEquals("part1", item.getFieldName()); assertEquals("{\"string\":\"foo\"}", item.getString()); item = items.get(1); assertTrue(item.isFormField()); assertEquals("part2", item.getFieldName()); // With developer builds we get: <MyBean><string>foo</string></MyBean> // But on CI server we get: <MyBean xmlns=""><string>foo</string></MyBean> // So... we make a compromise: assertThat(item.getString(), allOf(startsWith("<MyBean"), endsWith("><string>foo</string></MyBean>"))); }
private EBMSMessageHistoryRequestData getHttpRequestData() throws Exception { // Check content Type String contentType = monitor.getContentType(); if(contentType == null){ System.out.println((monitor==null?"Null Monitor":"Monitor not null")); System.out.println("Lengeth " + monitor.getContentLength()); Assert.fail("Null Content"); } String mediaType = contentType.split(";")[0]; assertEquals("Invalid content type", "text/xml", mediaType); // Check the multi-part content. // Make a request context that bridge the content from our monitor to FileUpload library. RequestContext rc = new RequestContext(){ public String getCharacterEncoding() { return "charset=utf-8"; } public int getContentLength() { return monitor.getContentLength(); } public String getContentType() { return monitor.getContentType(); } public InputStream getInputStream() { return monitor.getInputStream(); } }; BufferedReader bReader = new BufferedReader(new InputStreamReader(monitor.getInputStream())); String strLine = ""; do{ strLine = bReader.readLine(); }while(!strLine.contains("SOAP-ENV")); MimeHeaders header = new MimeHeaders(); header.addHeader("Content-Type", "text/xml"); SOAPMessage msg = MessageFactory.newInstance().createMessage( header, new ByteArrayInputStream(strLine.getBytes())); EBMSMessageHistoryRequestData data = new EBMSMessageHistoryRequestData(); data.setMessageId(getElementValue(msg.getSOAPBody(), "tns:messageId")); data.setMessageBox(getElementValue(msg.getSOAPBody(), "tns:messageBox")); data.setStatus(getElementValue(msg.getSOAPBody(), "tns:status")); data.setService(getElementValue(msg.getSOAPBody(), "tns:service")); data.setAction(getElementValue(msg.getSOAPBody(), "tns:action")); data.setConversationId(getElementValue(msg.getSOAPBody(), "tns:conversationId")); data.setCpaId(getElementValue(msg.getSOAPBody(), "tns:cpaId")); return data; }
public void processUpload(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { String hasHotDeployment = (String) configContext.getAxisConfiguration().getParameterValue("hotdeployment"); String hasHotUpdate = (String) configContext.getAxisConfiguration().getParameterValue("hotupdate"); req.setAttribute("hotDeployment", (hasHotDeployment.equals("true")) ? "enabled" : "disabled"); req.setAttribute("hotUpdate", (hasHotUpdate.equals("true")) ? "enabled" : "disabled"); RequestContext reqContext = new ServletRequestContext(req); boolean isMultipart = ServletFileUpload.isMultipartContent(reqContext); if (isMultipart) { try { //Create a factory for disk-based file items FileItemFactory factory = new DiskFileItemFactory(); //Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); List<?> items = upload.parseRequest(req); // Process the uploaded items Iterator<?> iter = items.iterator(); while (iter.hasNext()) { FileItem item = (FileItem) iter.next(); if (!item.isFormField()) { String fileName = item.getName(); String fileExtesion = fileName; fileExtesion = fileExtesion.toLowerCase(); if (!(fileExtesion.endsWith(".jar") || fileExtesion.endsWith(".aar"))) { req.setAttribute("status", "failure"); req.setAttribute("cause", "Unsupported file type " + fileExtesion); } else { String fileNameOnly; if (fileName.indexOf("\\") < 0) { fileNameOnly = fileName.substring(fileName.lastIndexOf("/") + 1, fileName .length()); } else { fileNameOnly = fileName.substring(fileName.lastIndexOf("\\") + 1, fileName .length()); } File uploadedFile = new File(serviceDir, fileNameOnly); item.write(uploadedFile); req.setAttribute("status", "success"); req.setAttribute("filename", fileNameOnly); } } } } catch (Exception e) { req.setAttribute("status", "failure"); req.setAttribute("cause", e.getMessage()); } } renderView("upload.jsp", req, res); }