List of usage examples for org.apache.commons.httpclient.methods.multipart MultipartRequestEntity MultipartRequestEntity
public MultipartRequestEntity(Part[] paramArrayOfPart, HttpMethodParams paramHttpMethodParams)
From source file:com.mycompany.semconsolewebapp.FileUpload.java
public static void uploadImage(String urlString, Part[] parts) throws FileNotFoundException, IOException { PostMethod postMessage = new PostMethod(urlString); postMessage.setRequestEntity(new MultipartRequestEntity(parts, postMessage.getParams())); HttpClient client = new HttpClient(); int status = client.executeMethod(postMessage); System.out.println("Got status message " + status); }
From source file:com.utest.domain.service.util.FileUploadUtil.java
public static void uploadFile(final File targetFile, final String targetURL, final String targerFormFieldName_) throws Exception { final PostMethod filePost = new PostMethod(targetURL); filePost.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, true); filePost.addRequestHeader("X-Atlassian-Token", "no-check"); try {/* w w w. j a va 2 s .c om*/ final FilePart fp = new FilePart(targerFormFieldName_, targetFile); fp.setTransferEncoding(null); final Part[] parts = { fp }; filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams())); final HttpClient client = new HttpClient(); client.getHttpConnectionManager().getParams().setConnectionTimeout(5000); final int status = client.executeMethod(filePost); if (status == HttpStatus.SC_OK) { Logger.getLogger(FileUploadUtil.class) .info("Upload complete, response=" + filePost.getResponseBodyAsString()); } else { Logger.getLogger(FileUploadUtil.class) .info("Upload failed, response=" + HttpStatus.getStatusText(status)); } } catch (final Exception ex) { Logger.getLogger(FileUploadUtil.class) .error("ERROR: " + ex.getClass().getName() + " " + ex.getMessage(), ex); throw ex; } finally { Logger.getLogger(FileUploadUtil.class).debug(new String(filePost.getResponseBody())); filePost.releaseConnection(); } }
From source file:net.formio.servlet.MockServletRequests.java
/** * Creates new servlet request that contains given resource as multi part. * @param paramName/*w ww. j av a 2 s.c o m*/ * @param resourceName * @return */ public static MockHttpServletRequest newRequest(String paramName, String resourceName, String mimeType) { try { MockHttpServletRequest request = new MockHttpServletRequest(); // Load resource being uploaded ByteArrayOutputStream bos = new ByteArrayOutputStream(); Streams.copy(MockServletRequests.class.getResourceAsStream(resourceName), bos, true); byte[] fileContent = bos.toByteArray(); // Create part & entity from resource Part[] parts = new Part[] { new FilePart(paramName, new ByteArrayPartSource(resourceName, fileContent), mimeType, (String) null) }; MultipartRequestEntity multipartRequestEntity = new MultipartRequestEntity(parts, new PostMethod().getParams()); ByteArrayOutputStream requestContent = new ByteArrayOutputStream(); multipartRequestEntity.writeRequest(requestContent); request.setContent(requestContent.toByteArray()); // Set content type of request (important, includes MIME boundary string) String contentType = multipartRequestEntity.getContentType(); request.setContentType(contentType); request.setMethod("POST"); return request; } catch (IOException ex) { throw new RuntimeException(ex.getMessage(), ex); } }
From source file:net.formio.portlet.MockPortletRequests.java
/** * Creates new portlet request that contains given resource as multi part. * @param paramName//from w w w .j a v a 2 s. co m * @param resourceName * @return */ public static MockMultipartActionRequest newRequest(String paramName, String resourceName, String mimeType) { try { MockMultipartActionRequest request = new MockMultipartActionRequest(); // Load resource being uploaded ByteArrayOutputStream bos = new ByteArrayOutputStream(); Streams.copy(MockPortletRequests.class.getResourceAsStream(resourceName), bos, true); byte[] fileContent = bos.toByteArray(); // Create part & entity from resource Part[] parts = new Part[] { new FilePart(paramName, new ByteArrayPartSource(resourceName, fileContent), mimeType, (String) null) }; MultipartRequestEntity multipartRequestEntity = new MultipartRequestEntity(parts, new PostMethod().getParams()); ByteArrayOutputStream requestContent = new ByteArrayOutputStream(); multipartRequestEntity.writeRequest(requestContent); request.setContent(requestContent.toByteArray()); // Set content type of request (important, includes MIME boundary string) String contentType = multipartRequestEntity.getContentType(); request.setContentType(contentType); return request; } catch (IOException ex) { throw new RuntimeException(ex.getMessage(), ex); } }
From source file:com.infosupport.service.LPRServiceCaller.java
public static void postData(String urlString, File file) { try {/* ww w .j a va2 s . com*/ PostMethod postMessage = new PostMethod(urlString); Part[] parts = { new FilePart("lpimage", file), new StringPart("country", "eu"), new StringPart("nonce", "123456789") }; postMessage.setRequestEntity(new MultipartRequestEntity(parts, postMessage.getParams())); HttpClient client = new HttpClient(); client.executeMethod(postMessage); } catch (IOException e) { Log.w(TAG, "IOException, waarschijnlijk geen internet connectie aanwezig..."); } }
From source file:ca.uvic.cs.tagsea.wizards.TagNetworkSender.java
public static synchronized void send(String xml, IProgressMonitor sendMonitor, int id) throws InvocationTargetException { sendMonitor.beginTask("Uploading Tags", 100); sendMonitor.subTask("Saving Temporary file..."); File location = TagSEAPlugin.getDefault().getStateLocation().toFile(); File temp = new File(location, "tagsea.temp.file.txt"); if (temp.exists()) { String message = "Unable to send tags. Unable to create temporary file."; IOException ex = new IOException(message); throw (new InvocationTargetException(ex, message)); }//ww w. ja va 2s . c om try { FileWriter writer = new FileWriter(temp); writer.write(xml); writer.flush(); writer.close(); } catch (IOException e) { throw new InvocationTargetException(e); } sendMonitor.worked(5); sendMonitor.subTask("Uploading Tags..."); String uploadScript = "http://stgild.cs.uvic.ca/cgi-bin/tagSEAUpload.cgi"; PostMethod post = new PostMethod(uploadScript); String fileName = getToday() + ".txt"; try { Part[] parts = { new StringPart("KIND", "tag"), new FilePart("MYLAR" + id, fileName, temp) }; post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams())); HttpClient client = new HttpClient(); int status = client.executeMethod(post); String resp = getData(post.getResponseBodyAsStream()); if (status != 200) { IOException ex = new IOException(resp); throw (ex); } } catch (IOException e) { throw new InvocationTargetException(e, e.getLocalizedMessage()); } finally { sendMonitor.worked(90); sendMonitor.subTask("Deleting Temporary File"); temp.delete(); sendMonitor.done(); } }
From source file:net.morphbank.mbsvc3.test.SendImageTest.java
public void sendImage(String strURL, String id, String originalFileName, String imageFileName) throws Exception { File input = new File(imageFileName); // Prepare HTTP post PostMethod post = new PostMethod(strURL); Part[] parts = { new StringPart("id", id), new StringPart("fileName", originalFileName), new FilePart("image", originalFileName, input) }; RequestEntity entity = new MultipartRequestEntity(parts, post.getParams()); post.setRequestEntity(entity);//from ww w .j av a 2 s . co m // Get HTTP client HttpClient httpclient = new HttpClient(); // Execute request try { System.out.println("Trying post"); int result = httpclient.executeMethod(post); // Display status code System.out.println("Response status code: " + result); // Display response System.out.println("Response body: "); InputStream response = post.getResponseBodyAsStream(); // int j = response.read(); System.out.write(j); for (int i = response.read(); i != -1; i = response.read()) { System.out.write(i); } // System.out.flush(); } finally { // Release current connection to the connection pool once you are // done post.releaseConnection(); } }
From source file:de.laeubisoft.tools.ant.validation.Tools.java
/** * Creates a {@link RequestEntity} that can be used for submitting a file * /*from w w w . j ava2s. com*/ * @param params * the params to use * @param methodParams * the {@link HttpMethodParams} of the requesting method * @return {@link RequestEntity} that can be used for submitting the given * file via Multipart * @throws IOException * if something is wrong with the file... */ public static RequestEntity createFileUpload(File file, String filePartName, String charset, List<NameValuePair> params, HttpMethodParams methodParams) throws IOException { if (file == null) { throw new FileNotFoundException("file not present!"); } List<Part> parts = nvToParts(params); FilePart fp = new FilePart(filePartName, file); fp.setContentType(URLConnection.guessContentTypeFromName(file.getName())); if (charset != null) { fp.setCharSet(charset); } parts.add(fp); return new MultipartRequestEntity(parts.toArray(new Part[0]), methodParams); }
From source file:ca.uvic.cs.tagsea.monitor.NetworkLogging.java
public synchronized boolean uploadForDate(Date d) throws IOException { File f = SimpleDayLog.findLogFileForDay(d); if (f.length() == 0) return true; PostMethod post = new PostMethod(uploadScript); Display disp = TagSEAMonitorPlugin.getDefault().getWorkbench().getDisplay(); int id = getUID(); if (id < 0) { //error getting the user id. Quit. return false; }//from w w w.j a v a 2s . c om Part[] parts = { new StringPart("KIND", "log"), new FilePart("MYLAR" + id, f.getName(), f) }; post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams())); HttpClient client = new HttpClient(); int status = client.executeMethod(post); resp = getData(post.getResponseBodyAsStream()); if (status != 200) { disp.syncExec(new Runnable() { public void run() { MessageDialog.openError(null, "Error Sending File", resp); } }); return false; } MonitorPreferences.setLastSendDate(d); return true; }
From source file:net.morphbank.webclient.RemoteImageProcessing.java
public void sendImage(String strURL, String id, String originalFileName, String imageFileName) throws Exception { File input = new File(imageFileName); // Prepare HTTP post PostMethod post = new PostMethod(strURL); Part[] parts = { new StringPart("id", id), new StringPart("fileName", originalFileName), new FilePart("image", originalFileName, input) }; RequestEntity entity = new MultipartRequestEntity(parts, post.getParams()); post.setRequestEntity(entity);//from w w w . j a va 2 s. co m // Get HTTP client HttpClient httpclient = new HttpClient(); // Execute request try { System.out.println("Trying post"); int postStatus = httpclient.executeMethod(post); // Display status code System.out.println("Response status code: " + postStatus); // Display response System.out.print("Response body: "); InputStream response = post.getResponseBodyAsStream(); for (int i = response.read(); i != -1; i = response.read()) { System.out.write(i); } System.out.flush(); } finally { // Release current connection to the connection pool once you are // done post.releaseConnection(); } }