List of usage examples for org.apache.commons.fileupload MultipartStream MultipartStream
public MultipartStream(InputStream input, byte[] boundary) throws IOException
Constructs a MultipartStream
with a default size buffer.
From source file:org.eclipse.che.api.builder.internal.SourcesManagerImpl.java
private void download(String downloadUrl, java.io.File downloadTo) throws IOException { HttpURLConnection conn = null; try {/*from ww w . j a v a 2 s.c om*/ final LinkedList<java.io.File> q = new LinkedList<>(); q.add(downloadTo); final long start = System.currentTimeMillis(); final List<Pair<String, String>> md5sums = new LinkedList<>(); while (!q.isEmpty()) { java.io.File current = q.pop(); java.io.File[] list = current.listFiles(); if (list != null) { for (java.io.File f : list) { if (f.isDirectory()) { q.push(f); } else { md5sums.add(Pair.of(com.google.common.io.Files.hash(f, Hashing.md5()).toString(), downloadTo.toPath().relativize(f.toPath()).toString().replace("\\", "/"))); //Replacing of "\" is need for windows support } } } } final long end = System.currentTimeMillis(); if (md5sums.size() > 0) { LOG.debug("count md5sums of {} files, time: {}ms", md5sums.size(), (end - start)); } conn = (HttpURLConnection) new URL(downloadUrl).openConnection(); conn.setConnectTimeout(CONNECT_TIMEOUT); conn.setReadTimeout(READ_TIMEOUT); final EnvironmentContext context = EnvironmentContext.getCurrent(); if (context.getUser() != null && context.getUser().getToken() != null) { conn.setRequestProperty(HttpHeaders.AUTHORIZATION, context.getUser().getToken()); } if (!md5sums.isEmpty()) { conn.setRequestMethod(HttpMethod.POST); conn.setRequestProperty("Content-type", MediaType.TEXT_PLAIN); conn.setRequestProperty(HttpHeaders.ACCEPT, MediaType.MULTIPART_FORM_DATA); conn.setDoOutput(true); try (OutputStream output = conn.getOutputStream(); Writer writer = new OutputStreamWriter(output)) { for (Pair<String, String> pair : md5sums) { writer.write(pair.first); writer.write(' '); writer.write(pair.second); writer.write('\n'); } } } final int responseCode = conn.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { final String contentType = conn.getHeaderField("content-type"); if (contentType.startsWith(MediaType.MULTIPART_FORM_DATA)) { final HeaderParameterParser headerParameterParser = new HeaderParameterParser(); final String boundary = headerParameterParser.parse(contentType).get("boundary"); try (InputStream in = conn.getInputStream()) { MultipartStream multipart = new MultipartStream(in, boundary.getBytes()); boolean hasMore = multipart.skipPreamble(); while (hasMore) { final Map<String, List<String>> headers = parseChunkHeader( CharStreams.readLines(new StringReader(multipart.readHeaders()))); final List<String> contentDisposition = headers.get("content-disposition"); final String name = headerParameterParser.parse(contentDisposition.get(0)).get("name"); if ("updates".equals(name)) { int length = -1; List<String> contentLengthHeader = headers.get("content-length"); if (contentLengthHeader != null && !contentLengthHeader.isEmpty()) { length = Integer.parseInt(contentLengthHeader.get(0)); } if (length < 0 || length > 204800) { java.io.File tmp = java.io.File.createTempFile("tmp", ".zip", directory); try { try (FileOutputStream fOut = new FileOutputStream(tmp)) { multipart.readBodyData(fOut); } ZipUtils.unzip(tmp, downloadTo); } finally { if (tmp.exists()) { tmp.delete(); } } } else { final ByteArrayOutputStream bOut = new ByteArrayOutputStream(length); multipart.readBodyData(bOut); ZipUtils.unzip(new ByteArrayInputStream(bOut.toByteArray()), downloadTo); } } else if ("removed-paths".equals(name)) { final ByteArrayOutputStream bOut = new ByteArrayOutputStream(); multipart.readBodyData(bOut); final String[] removed = JsonHelper.fromJson( new ByteArrayInputStream(bOut.toByteArray()), String[].class, null); for (String path : removed) { java.io.File f = new java.io.File(downloadTo, path); if (!f.delete()) { throw new IOException(String.format("Unable delete %s", path)); } } } else { // To /dev/null :) multipart.readBodyData(DEV_NULL); } hasMore = multipart.readBoundary(); } } } else { try (InputStream in = conn.getInputStream()) { ZipUtils.unzip(in, downloadTo); } } } else if (responseCode != HttpURLConnection.HTTP_NO_CONTENT) { throw new IOException( String.format("Invalid response status %d from remote server. ", responseCode)); } } catch (ParseException | JsonParseException e) { throw new IOException(e.getMessage(), e); } finally { if (conn != null) { conn.disconnect(); } } }
From source file:org.jwifisd.eyefi.Main3.java
public static void main(String[] args) throws Exception { MultipartStream stream = new MultipartStream( new FileInputStream("src/main/resources/NanoHTTPD-977513220698581430"), "---------------------------02468ace13579bdfcafebabef00d".getBytes()); ByteArrayOutputStream output = new ByteArrayOutputStream(); String readHeaders = stream.readHeaders(); System.out.println(readHeaders.toString()); stream.readBodyData(output);//from w w w .j a v a 2 s . c o m output = new ByteArrayOutputStream(); readHeaders = stream.readHeaders(); System.out.println(readHeaders.toString()); stream.readBodyData(output); final InputStream in = new FileInputStream("src/main/resources/NanoHTTPD-977513220698581430"); FileItemIterator iter = new FileUpload().getItemIterator(new RequestContext() { @Override public InputStream getInputStream() throws IOException { return in; } @Override public String getContentType() { // TODO Auto-generated method stub return "multipart/form-data; boundary=---------------------------02468ace13579bdfcafebabef00d"; } @Override public int getContentLength() { return 4237763; } @Override public String getCharacterEncoding() { // TODO Auto-generated method stub return "UTF-8"; } }); while (iter.hasNext()) { FileItemStream item = iter.next(); System.out.println("name:" + item.getName()); System.out.println("name:" + item.getContentType()); //item.openStream(); } }
From source file:org.rsna.ctp.servlets.LookupServlet.java
/** * The servlet method that responds to an HTTP POST. * This method interprets the posted parameters as a new addition * to the file and updates it accordingly. * It then returns an HTML page containing a new form constructed * from the new contents of the file.// ww w .j a v a2s. co m * @param req The HttpRequest provided by the servlet container. * @param res The HttpResponse provided by the servlet container. */ public void doPost(HttpRequest req, HttpResponse res) { //Make sure the user is authorized to do this. String home = req.getParameter("home", "/"); if (!req.userHasRole("admin")) { res.redirect(home); return; } //Get the parameters from the form. String phi = req.getParameter("phi"); String replacement = req.getParameter("replacement"); int p, s; File file = null; try { p = Integer.parseInt(req.getParameter("p")); s = Integer.parseInt(req.getParameter("s")); file = getLookupTableFile(p, s); //Update the file if possible. synchronized (this) { if ((phi != null) && (replacement != null) && (file != null)) { Properties props = getProperties(file); phi = phi.trim(); replacement = replacement.trim(); if (!phi.equals("")) { props.setProperty(phi, replacement); saveProperties(props, file); } //Make a new page from the new data and send it out res.disableCaching(); res.setContentType("html"); res.write(getEditorPage(p, s, file, home)); res.send(); return; } else if ((req.getContentType() != null) && (req.getContentType().indexOf("multipart/form-data") >= 0)) { //determining boundary bytes int boundaryIndex = req.getContentType().indexOf("boundary="); byte[] boundary = (req.getContentType().substring(boundaryIndex + 9)).getBytes(); //initiate the multipart stream (parser @SuppressWarnings("deprecation") MultipartStream multipartStream = new MultipartStream(req.getInputStream(), boundary); //parse the data while skipping the preamble boolean nextPart = multipartStream.skipPreamble(); String fileString = null; //check if first (next) file is available if (nextPart) { //read headers (to flush out of stream) multipartStream.readHeaders(); //read body data ByteArrayOutputStream data = new ByteArrayOutputStream(); multipartStream.readBodyData(data); fileString = new String(data.toByteArray()); //see whether there is a next part (next file) nextPart = multipartStream.readBoundary(); } if (fileString != null) { //split the data according to the line breaks String[] files = null; if (fileString.contains("\r\n")) files = fileString.split("\r\n"); else if (fileString.contains("\n")) files = fileString.split("\n"); //get the number of lines int numberOfLines = files.length; //get the properties file Properties props = getProperties(file); //loop through every line in the uploaded csv file for (int i = 0; i < numberOfLines; i++) { String lookupString = files[i]; String[] lookupSplit = null; //split columns of csv based on semicolon (;) or comma (,) // as this behaviour is different for different versions of office/excel if (lookupString.contains(";")) lookupSplit = lookupString.split(";"); else if (lookupString.contains(",")) lookupSplit = lookupString.split(","); //if columns arent's split, do not continue if (lookupSplit != null) { //if first column does not contain "pts/", add it, otherwise just add the property props.setProperty(lookupSplit[0].trim(), lookupSplit[1].trim()); } else { logger.warn("Line " + i + 1 + " (starting from 1) cannot be split using semicolon or comma"); } //save the properties in the script file saveProperties(props, file); } } else { logger.warn("uploaded file is empty"); } } } res.disableCaching(); res.setContentType("html"); res.write(getEditorPage(p, s, file, home)); res.send(); return; } catch (Exception ex) { } res.setResponseCode(500); //Unable to perform the function. res.send(); }
From source file:org.sapia.soto.state.cocoon.util.CocoonFileUploadBase.java
/** * Processes an <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867 </a> * compliant <code>multipart/form-data</code> stream. If files are stored on * disk, the path is given by <code>getRepository()</code>. * /*from w w w . ja va 2s . co m*/ * @param req * The servlet request to be parsed. * * @return A list of <code>FileItem</code> instances parsed from the * request, in the order that they were transmitted. * * @exception FileUploadException * if there are problems reading/parsing the request or storing * files. */ public List /* FileItem */ parseRequest(HttpRequest req) throws FileUploadException { if (null == req) { throw new NullPointerException("req parameter"); } ArrayList items = new ArrayList(); String contentType = req.getHeader(CONTENT_TYPE); if ((null == contentType) || (!contentType.startsWith(MULTIPART))) { throw new InvalidContentTypeException("the request doesn't contain a " + MULTIPART_FORM_DATA + " or " + MULTIPART_MIXED + " stream, content type header is " + contentType); } int requestSize = req.getContentLength(); if (requestSize == -1) { throw new UnknownSizeException("the request was rejected because it's size is unknown"); } if ((sizeMax >= 0) && (requestSize > sizeMax)) { throw new SizeLimitExceededException( "the request was rejected because " + "it's size exceeds allowed range"); } try { int boundaryIndex = contentType.indexOf("boundary="); if (boundaryIndex < 0) { throw new FileUploadException( "the request was rejected because " + "no multipart boundary was found"); } byte[] boundary = contentType.substring(boundaryIndex + 9).getBytes(); InputStream input = req.getInputStream(); MultipartStream multi = new MultipartStream(input, boundary); multi.setHeaderEncoding(headerEncoding); boolean nextPart = multi.skipPreamble(); while (nextPart) { Map headers = parseHeaders(multi.readHeaders()); String fieldName = getFieldName(headers); if (fieldName != null) { String subContentType = getHeader(headers, CONTENT_TYPE); if ((subContentType != null) && subContentType.startsWith(MULTIPART_MIXED)) { // Multiple files. byte[] subBoundary = subContentType.substring(subContentType.indexOf("boundary=") + 9) .getBytes(); multi.setBoundary(subBoundary); boolean nextSubPart = multi.skipPreamble(); while (nextSubPart) { headers = parseHeaders(multi.readHeaders()); if (getFileName(headers) != null) { FileItem item = createItem(headers, false); OutputStream os = item.getOutputStream(); try { multi.readBodyData(os); } finally { os.close(); } items.add(item); } else { // Ignore anything but files inside // multipart/mixed. multi.discardBodyData(); } nextSubPart = multi.readBoundary(); } multi.setBoundary(boundary); } else { FileItem item = createItem(headers, getFileName(headers) == null); OutputStream os = item.getOutputStream(); try { multi.readBodyData(os); } finally { os.close(); } items.add(item); } } else { // Skip this part. multi.discardBodyData(); } nextPart = multi.readBoundary(); } } catch (IOException e) { throw new FileUploadException( "Processing of " + MULTIPART_FORM_DATA + " request failed. " + e.getMessage()); } return items; }
From source file:project.cs.lisa.netinf.node.resolution.NameResolutionService.java
private InformationObject readIoAndFile(Identifier identifier, HttpResponse response) throws InvalidResponseException { Log.d(TAG, "readIoAndFile()"); if (response == null) { throw new InvalidResponseException("Response is null."); } else if (response.getEntity() == null) { throw new InvalidResponseException("Entity is null."); } else if (response.getEntity().getContentType() == null) { throw new InvalidResponseException("Content-Type is null."); } else if (!response.getEntity().getContentType().getValue().startsWith("multipart/form-data")) { throw new InvalidResponseException("Content-Type is " + response.getEntity().getContentType().getValue() + ", expected to start with \"multipart/form-data\""); }//from w ww. j a v a2s.c o m try { // Create IO InformationObject io = mDatamodelFactory.createInformationObject(); io.setIdentifier(identifier); Log.d(TAG, "name: " + response.getHeaders("Content-Type")[0].getName()); Log.d(TAG, "value: " + response.getHeaders("Content-Type")[0].getValue()); String contentType = response.getHeaders("Content-Type")[0].getValue(); byte[] boundary = (contentType.substring(contentType.indexOf("boundary=") + 9)).getBytes(); Log.d(TAG, "boundary = " + Arrays.toString(boundary)); @SuppressWarnings("deprecation") MultipartStream multipartStream = new MultipartStream(response.getEntity().getContent(), boundary); multipartStream.skipPreamble(); // TODO Dependant on order used by NRS // Read JSON ByteArrayOutputStream jsonStream = new ByteArrayOutputStream(); multipartStream.readHeaders(); multipartStream.readBodyData(jsonStream); multipartStream.readBoundary(); JSONObject jsonObject = parseJson(jsonStream.toString()); jsonStream.close(); addContentType(io.getIdentifier(), jsonObject); addMetadata(io.getIdentifier(), jsonObject); addLocators(io, jsonObject); File file = new File( Environment.getExternalStorageDirectory() + "/DCIM/Shared/" + getHash(io.getIdentifier())); FileOutputStream fos = new FileOutputStream(file); multipartStream.readHeaders(); multipartStream.readBodyData(fos); fos.flush(); fos.close(); // Add file path locator Attribute locator = mDatamodelFactory.createAttribute(); locator.setAttributePurpose(DefinedAttributePurpose.LOCATOR_ATTRIBUTE.toString()); locator.setIdentification(SailDefinedAttributeIdentification.FILE_PATH.getURI()); locator.setValue(file.getAbsoluteFile()); io.addAttribute(locator); return io; } catch (IOException e) { throw new InvalidResponseException("Failed to read InformationObject from response", e); } }
From source file:project.cs.netinfservice.netinf.node.resolution.NameResolutionService.java
/** * Creates an Information Object and file from a previous HTTP request. * * @param identifier/* ww w . ja v a 2 s. co m*/ * The identifier * @param response * HTTP response * @return * New Information Object created from the response. * <p>A side-effect is the new file created.</p> * @throws InvalidResponseException * Thrown if the response had an invalid information object. */ private InformationObject readIoAndFile(Identifier identifier, HttpResponse response) throws InvalidResponseException { // Sanity checks the response. if (response == null) { throw new InvalidResponseException("Response is null."); } else if (response.getEntity() == null) { throw new InvalidResponseException("Entity is null."); } else if (response.getEntity().getContentType() == null) { throw new InvalidResponseException("Content-Type is null."); } else if (!response.getEntity().getContentType().getValue().startsWith("multipart/form-data")) { throw new InvalidResponseException("Content-Type is " + response.getEntity().getContentType().getValue() + ", expected to start with \"multipart/form-data\""); } // Reads IO and File try { // Create IO InformationObject io = mDatamodelFactory.createInformationObject(); io.setIdentifier(identifier); // Extract Content-type from header String contentType = response.getHeaders("Content-Type")[0].getValue(); // Get boundary bytes byte[] boundary = (contentType.substring(contentType.indexOf("boundary=") + 9)).getBytes(); // Raises intent Intent intent = new Intent(NRS_TRANSMISSION); MainNetInfActivity.getActivity().sendBroadcast(intent); // Start multipart @SuppressWarnings("deprecation") MultipartStream multipartStream = new MultipartStream(response.getEntity().getContent(), boundary); // Skip multipart preamble multipartStream.skipPreamble(); // TODO Dependant on order used by NRS // Read JSON ByteArrayOutputStream jsonStream = new ByteArrayOutputStream(); // Move on multipart stream multipartStream.readHeaders(); multipartStream.readBodyData(jsonStream); multipartStream.readBoundary(); // Parse JSON Object JSONObject jsonObject = parseJson(jsonStream.toString()); // Close stream used to read JSON jsonStream.close(); // Add attributes to the new Information Object addContentType(io.getIdentifier(), jsonObject); addMetadata(io.getIdentifier(), jsonObject); addLocators(io, jsonObject); // Create the new file File file = new File( Environment.getExternalStorageDirectory() + "/DCIM/Shared/" + getHash(io.getIdentifier())); // Write file in disk FileOutputStream fos = new FileOutputStream(file); // move on Multipart multipartStream.readHeaders(); multipartStream.readBodyData(fos); // Close file stream fos.flush(); fos.close(); // Add file path locator Attribute locator = mDatamodelFactory.createAttribute(); locator.setAttributePurpose(DefinedAttributePurpose.LOCATOR_ATTRIBUTE.toString()); locator.setIdentification(SailDefinedAttributeIdentification.FILE_PATH.getURI()); locator.setValue(file.getAbsoluteFile()); // Add atributes io.addAttribute(locator); // Return new Information Object created return io; } catch (IOException e) { throw new InvalidResponseException("Failed to read InformationObject from response", e); } }
From source file:rocks.inspectit.ui.rcp.storage.util.DataRetriever.java
/** * Retrieves the wanted data described in the {@link StorageDescriptor} from the desired * {@link CmrRepositoryDefinition}. This method will try to invoke as less as possible HTTP * requests for all descriptors./*from ww w.j av a 2s.c o m*/ * <p> * The method will execute the HTTP requests sequentially. * <p> * It is not guaranteed that amount of returned objects in the list is same as the amount of * provided descriptors. If some of the descriptors are pointing to the wrong files or files * positions, it can happen that this influences the rest of the descriptor that point to the * same file. Thus, a special care needs to be taken that the data in descriptors is correct. * * @param <E> * Type of the objects are wanted. * @param cmrRepositoryDefinition * {@link CmrRepositoryDefinition}. * @param storageData * {@link StorageData} that points to the wanted storage. * @param descriptors * Descriptors. * @return List of objects in the supplied generic type. Note that if the data described in the * descriptor is not of a supplied generic type, there will be a casting exception * thrown. * @throws SerializationException * If {@link SerializationException} occurs. * @throws IOException * If {@link IOException} occurs. */ @SuppressWarnings("unchecked") public <E extends DefaultData> List<E> getDataViaHttp(CmrRepositoryDefinition cmrRepositoryDefinition, IStorageData storageData, List<IStorageDescriptor> descriptors) throws IOException, SerializationException { Map<Integer, List<IStorageDescriptor>> separateFilesGroup = createFilesGroup(descriptors); List<E> receivedData = new ArrayList<>(); String serverUri = getServerUri(cmrRepositoryDefinition); HttpClient httpClient = new DefaultHttpClient(); for (Map.Entry<Integer, List<IStorageDescriptor>> entry : separateFilesGroup.entrySet()) { HttpGet httpGet = new HttpGet( serverUri + storageManager.getHttpFileLocation(storageData, entry.getKey())); StringBuilder rangeHeader = new StringBuilder("bytes="); RangeDescriptor rangeDescriptor = null; for (IStorageDescriptor descriptor : entry.getValue()) { if (null == rangeDescriptor) { rangeDescriptor = new RangeDescriptor(descriptor); } else { if ((rangeDescriptor.getEnd() + 1) == descriptor.getPosition()) { rangeDescriptor.setEnd((descriptor.getPosition() + descriptor.getSize()) - 1); } else { rangeHeader.append(rangeDescriptor.toString()); rangeHeader.append(','); rangeDescriptor = new RangeDescriptor(descriptor); } } } rangeHeader.append(rangeDescriptor); httpGet.addHeader("Range", rangeHeader.toString()); ISerializer serializer = null; try { serializer = serializerQueue.take(); } catch (InterruptedException e) { Thread.interrupted(); } InputStream inputStream = null; Input input = null; try { HttpResponse response = httpClient.execute(httpGet); HttpEntity entity = response.getEntity(); if (MultipartEntityUtil.isMultipart(entity)) { inputStream = entity.getContent(); @SuppressWarnings("deprecation") // all non-deprecated constructors have default modifier MultipartStream multipartStream = new MultipartStream(inputStream, MultipartEntityUtil.getBoundary(entity).getBytes()); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); boolean nextPart = multipartStream.skipPreamble(); while (nextPart) { multipartStream.readHeaders(); multipartStream.readBodyData(byteArrayOutputStream); input = new Input(byteArrayOutputStream.toByteArray()); while (KryoUtil.hasMoreBytes(input)) { Object object = serializer.deserialize(input); E element = (E) object; receivedData.add(element); } nextPart = multipartStream.readBoundary(); } } else { // when kryo changes the visibility of optional() method, we can really stream input = new Input(EntityUtils.toByteArray(entity)); while (KryoUtil.hasMoreBytes(input)) { Object object = serializer.deserialize(input); E element = (E) object; receivedData.add(element); } } } finally { if (null != inputStream) { inputStream.close(); } if (null != input) { input.close(); } serializerQueue.add(serializer); } } return receivedData; }