@Override protected MultipartFile[] readInternal (Class<? extends MultipartFile[]> clazz, HttpInputMessage inputMessage ) throws IOException { val boundaryBytes = getMultiPartBoundary(inputMessage.getHeaders().getContentType()); MultipartStream multipartStream = new MultipartStream(inputMessage.getBody(), boundaryBytes, bufSize, null); val multiparts = new LinkedList<ByteArrayMultipartFile>(); for (boolean nextPart = multipartStream.skipPreamble(); nextPart; nextPart = multipartStream.readBoundary()) { ByteArrayMultipartFile multiPart; try { multiPart = readMultiPart(multipartStream); } catch (Exception e) { throw new HttpMessageNotReadableException("Multipart body could not be read.", e); } multiparts.add(multiPart); } return multiparts.toArray(new ByteArrayMultipartFile[multiparts.size()]); }
public void finaliseOnError(Throwable pEx) { mStatus = FAILED; mErrorException = pEx; if (mUploadInfo != null) { if(mUploadInfo.getStatus().isInProgress() || mUploadInfo.getStatus() == UploadStatus.NOT_STARTED) { mUploadInfo.setStatus(UploadStatus.FAILED); } mUploadInfo.setSystemMsg(XFUtil.getJavaStackTraceInfo(pEx)); } if(mItemInputStream != null && mItemInputStream instanceof MultipartStream.ItemInputStream) { //Forcibly close the underlying input stream for the file upload //This prevents threads getting stuck due to Tomcat/F5 issue caused by the client continuing to send data when FOX isn't reading it try { ((MultipartStream.ItemInputStream) mItemInputStream).close(true); } catch (Throwable th) { Track.recordSuppressedException("UploadFinaliseInputStreamClose", th); } } if(mItemNonBlockingInputStream != null) { mItemNonBlockingInputStream.destroy(); } }
private String writeConfigFile (InputStream inputStream, String b) throws IOException { byte[] boundary = b.getBytes(); @SuppressWarnings("deprecation") MultipartStream multipartStream = new MultipartStream(inputStream, boundary); File tempDir = new File(STORAGE_DIR_PATH); if (!tempDir.exists()) { tempDir.mkdir(); } File file = File.createTempFile(CONFIG_FILE_PREFIX, ".xml", tempDir); FileOutputStream out = new FileOutputStream(file.getAbsolutePath()); boolean nextPart = multipartStream.skipPreamble(); if (nextPart) { multipartStream.readHeaders(); multipartStream.readBodyData(out); } out.close(); String id = file.getName(); return id.substring(id.indexOf(CONFIG_FILE_PREFIX) + CONFIG_FILE_PREFIX.length(), id.lastIndexOf(".xml")); }
private ByteArrayMultipartFile readMultiPart (MultipartStream multipartStream) throws IOException { val multiPartHeaders = splitIntoKeyValuePairs( multipartStream.readHeaders(), NEWLINES_PATTERN, COLON_PATTERN, false ); val contentDisposition = splitIntoKeyValuePairs( multiPartHeaders.get(CONTENT_DISPOSITION), SEMICOLON_PATTERN, EQUALITY_SIGN_PATTERN, true ); if (!contentDisposition.containsKey("form-data")) { throw new HttpMessageNotReadableException("Content-Disposition is not of type form-data."); } val bodyStream = new ByteArrayOutputStream(); multipartStream.readBodyData(bodyStream); return new ByteArrayMultipartFile( contentDisposition.get("name"), contentDisposition.get("filename"), multiPartHeaders.get(CONTENT_TYPE), bodyStream.toByteArray() ); }
private static Struct getPartData(MultipartStream stream) throws IOException, PageException { Struct headers = extractHeaders(stream.readHeaders()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); stream.readBodyData(baos); Struct fileStruct = new StructImpl(); fileStruct.set(KeyConstants._content, baos.toByteArray()); fileStruct.set(KeyConstants._headers, headers); IOUtil.closeEL(baos); return fileStruct; }
public static byte[] processMultiPartFile(HttpServletRequest httpServletRequest, String contentMessage) throws IOException { //Content-Type: multipart/form-data; boundary="----=_Part_2_1843361794.1448198281814" String encoding = httpServletRequest.getCharacterEncoding(); if (encoding == null) { encoding = DEFAULT_ENCODING; httpServletRequest.setCharacterEncoding(encoding); } byte[] requestBodyArray = IOUtils.toByteArray(httpServletRequest.getInputStream()); if (requestBodyArray == null || requestBodyArray.length == 0) { throw new ActivitiIllegalArgumentException("No :" + contentMessage + "was found in request body."); } String requestContentType = httpServletRequest.getContentType(); StringBuilder contentTypeString = new StringBuilder(); contentTypeString.append("Content-Type: " + requestContentType); contentTypeString.append("\r"); contentTypeString.append(System.getProperty("line.separator")); byte[] contentTypeArray = contentTypeString.toString().getBytes(encoding); byte[] aggregatedRequestBodyByteArray = new byte[contentTypeArray.length + requestBodyArray.length]; System.arraycopy(contentTypeArray, 0, aggregatedRequestBodyByteArray, 0, contentTypeArray.length); System.arraycopy(requestBodyArray, 0, aggregatedRequestBodyByteArray, contentTypeArray .length, requestBodyArray.length); boolean debugEnabled = log.isDebugEnabled(); int index = requestContentType.indexOf("boundary"); if (index <= 0) { throw new ActivitiIllegalArgumentException("boundary tag not found in the request header."); } String boundaryString = requestContentType.substring(index + "boundary=".length()); boundaryString = boundaryString.replaceAll("\"", "").trim(); if (debugEnabled) { log.debug("----------Content-Type:-----------\n" + httpServletRequest.getContentType()); log.debug("\n\n\n\n"); log.debug("\n\n\n\n----------Aggregated Request Body:-----------\n" + new String(aggregatedRequestBodyByteArray)); log.debug("boundaryString:" + boundaryString); } byte[] boundary = boundaryString.getBytes(encoding); ByteArrayInputStream content = new ByteArrayInputStream(aggregatedRequestBodyByteArray); MultipartStream multipartStream = new MultipartStream(content, boundary, aggregatedRequestBodyByteArray.length, null); boolean nextPart = multipartStream.skipPreamble(); if (debugEnabled) { log.debug(nextPart); } ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); byte[] byteArray = null; // Get first file in the map, ignore possible other files while (nextPart) { // if (debugEnabled) { String header = multipartStream.readHeaders(); printHeaders(header); } multipartStream.readBodyData(byteArrayOutputStream); byteArray = byteArrayOutputStream.toByteArray(); nextPart = multipartStream.readBoundary(); } return byteArray; }