List of usage examples for org.apache.commons.httpclient.methods.multipart FilePartSource FilePartSource
public FilePartSource(File paramFile) throws FileNotFoundException
From source file:com.boyuanitsm.pay.alipay.util.AlipayCore.java
/** * ??//from w ww . ja va 2 s . c om * @param strFilePath * @param file_digest_type ? * @return ? */ public static String getAbstract(String strFilePath, String file_digest_type) throws IOException { PartSource file = new FilePartSource(new File(strFilePath)); if (file_digest_type.equals("MD5")) { return DigestUtils.md5Hex(file.createInputStream()); } else if (file_digest_type.equals("SHA")) { return DigestUtils.sha256Hex(file.createInputStream()); } else { return ""; } }
From source file:com.boyuanitsm.pay.alipay.util.httpClient.HttpProtocolHandler.java
/** * Http//from w w w . j a va 2s.c om * * @param request ? * @param strParaFileName ??? * @param strFilePath * @return * @throws HttpException, IOException */ public HttpResponse execute(HttpRequest request, String strParaFileName, String strFilePath) throws HttpException, IOException { HttpClient httpclient = new HttpClient(connectionManager); // int connectionTimeout = defaultConnectionTimeout; if (request.getConnectionTimeout() > 0) { connectionTimeout = request.getConnectionTimeout(); } httpclient.getHttpConnectionManager().getParams().setConnectionTimeout(connectionTimeout); // int soTimeout = defaultSoTimeout; if (request.getTimeout() > 0) { soTimeout = request.getTimeout(); } httpclient.getHttpConnectionManager().getParams().setSoTimeout(soTimeout); // ConnectionManagerconnection httpclient.getParams().setConnectionManagerTimeout(defaultHttpConnectionManagerTimeout); String charset = request.getCharset(); charset = charset == null ? DEFAULT_CHARSET : charset; HttpMethod method = null; //get?? if (request.getMethod().equals(HttpRequest.METHOD_GET)) { method = new GetMethod(request.getUrl()); method.getParams().setCredentialCharset(charset); // parseNotifyConfig??GETrequestQueryString method.setQueryString(request.getQueryString()); } else if (strParaFileName.equals("") && strFilePath.equals("")) { //post?? method = new PostMethod(request.getUrl()); ((PostMethod) method).addParameters(request.getParameters()); method.addRequestHeader("Content-Type", "application/x-www-form-urlencoded; text/html; charset=" + charset); } else { //post? method = new PostMethod(request.getUrl()); List<Part> parts = new ArrayList<Part>(); for (int i = 0; i < request.getParameters().length; i++) { parts.add(new StringPart(request.getParameters()[i].getName(), request.getParameters()[i].getValue(), charset)); } //?strParaFileName??? parts.add(new FilePart(strParaFileName, new FilePartSource(new File(strFilePath)))); // ((PostMethod) method).setRequestEntity( new MultipartRequestEntity(parts.toArray(new Part[0]), new HttpMethodParams())); } // Http HeaderUser-Agent method.addRequestHeader("User-Agent", "Mozilla/4.0"); HttpResponse response = new HttpResponse(); try { httpclient.executeMethod(method); if (request.getResultType().equals(HttpResultType.STRING)) { response.setStringResult(method.getResponseBodyAsString()); } else if (request.getResultType().equals(HttpResultType.BYTES)) { response.setByteResult(method.getResponseBody()); } response.setResponseHeaders(method.getResponseHeaders()); } catch (UnknownHostException ex) { return null; } catch (IOException ex) { return null; } catch (Exception ex) { return null; } finally { method.releaseConnection(); } return response; }
From source file:org.andrewberman.sync.PDFDownloader.java
/** * Uploads and downloads the bibtex library file, to synchronize library * changes./*from ww w . j a v a 2 s . c om*/ */ void syncBibTex() throws Exception { String base = baseDir.getCanonicalPath() + sep; itemMax = -1; final File bibFile = new File(base + username + ".bib"); // final File md5File = new File(base + ".md5s.txt"); final File dateFile = new File(base + "sync_info.txt"); // Check the MD5s, if the .md5s.txt file exists. long bibModified = bibFile.lastModified(); long lastDownload = 0; boolean download = true; boolean upload = true; if (dateFile.exists()) { String fileS = readFile(dateFile); String[] props = fileS.split("\n"); for (String prop : props) { String[] keyval = prop.split("="); if (keyval[0].equals("date")) { lastDownload = Long.valueOf(keyval[1]); } } } if (lastDownload >= bibModified) { upload = false; } boolean uploadSuccess = false; if (bibFile.exists() && uploadNewer && upload) { BufferedReader br = null; try { status("Uploading BibTex file..."); FilePartSource fsrc = new FilePartSource(bibFile); FilePart fp = new FilePart("file", fsrc); br = new BufferedReader(new FileReader(bibFile)); StringBuffer sb = new StringBuffer(); String s; while ((s = br.readLine()) != null) { sb.append(s + "\n"); } String str = sb.toString(); final Part[] parts = new Part[] { new StringPart("btn_bibtex", "Import BibTeX file..."), new StringPart("to_read", "2"), new StringPart("tag", ""), new StringPart("private", "t"), new StringPart("update_allowed", "t"), new StringPart("update_id", "cul-id"), new StringPart("replace_notes", "t"), new StringPart("replace_tags", "t"), fp }; waitOrExit(); PostMethod filePost = new PostMethod(BASE_URL + "/profile/" + username + "/import_do"); try { filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams())); httpclient.executeMethod(filePost); String response = filePost.getResponseBodyAsString(); // System.out.println(response); uploadSuccess = true; System.out.println("Bibtex upload success!"); } finally { filePost.releaseConnection(); } } catch (Exception e) { e.printStackTrace(); } finally { if (br != null) br.close(); } } if (download || upload) { status("Downloading BibTeX file..."); String pageURL = BASE_URL + "bibtex/user/" + username + ""; FileWriter fw = new FileWriter(bibFile); try { GetMethod get = new GetMethod(pageURL); httpclient.executeMethod(get); InputStreamReader read = new InputStreamReader(get.getResponseBodyAsStream()); int c; while ((c = read.read()) != -1) { waitOrExit(); fw.write(c); } read.close(); } finally { fw.close(); } } // Store the checksums. if (uploadSuccess) { // if (fileChecksum == null) // { // fileChecksum = getMd5(bibFile); // remoteChecksum = get(BASE_URL + "bibtex/user/" + username + "?md5=true"); // } // String md5S = fileChecksum + "\n" + remoteChecksum; // writeFile(md5File, md5S); String dateS = "date=" + Calendar.getInstance().getTimeInMillis(); writeFile(dateFile, dateS); } }
From source file:org.apache.commons.httpclient.methods.multipart.FilePart.java
/** * FilePart Constructor.//from ww w .ja v a 2 s .co m * * @param name the name of the file part * @param file the file to post * * @throws FileNotFoundException if the <i>file</i> is not a normal * file or if it is not readable. */ public FilePart(String name, File file) throws FileNotFoundException { this(name, new FilePartSource(file), null, null); }
From source file:org.apache.commons.httpclient.methods.multipart.FilePart.java
/** * FilePart Constructor.//from w w w . j ava 2 s. c o m * * @param name the name of the file part * @param file the file to post * @param contentType the content type for this part, if <code>null</code> the * {@link #DEFAULT_CONTENT_TYPE default} is used * @param charset the charset encoding for this part, if <code>null</code> the * {@link #DEFAULT_CHARSET default} is used * * @throws FileNotFoundException if the <i>file</i> is not a normal * file or if it is not readable. */ public FilePart(String name, File file, String contentType, String charset) throws FileNotFoundException { this(name, new FilePartSource(file), contentType, charset); }
From source file:org.loadosophia.client.LoadosophiaAPIClient.java
public LoadosophiaUploadResults sendFiles(File targetFile, LinkedList<String> perfMonFiles) throws IOException { LoadosophiaUploadResults results = new LoadosophiaUploadResults(); if (targetFile.length() == 0) { throw new IOException("Cannot send empty file to Loadosophia.org"); }//from w w w . j a v a2 s . co m log.info("Preparing files to send"); LinkedList<Part> partsList = new LinkedList<Part>(); partsList.add(new StringPart("projectKey", project)); partsList.add(new FilePart("jtl_file", new FilePartSource(gzipFile(targetFile)))); Iterator<String> it = perfMonFiles.iterator(); int index = 0; while (it.hasNext()) { File perfmonFile = new File(it.next()); if (!perfmonFile.exists()) { log.warn("File not exists, skipped: " + perfmonFile.getAbsolutePath()); continue; } partsList.add(new FilePart("perfmon_" + index, new FilePartSource(gzipFile(perfmonFile)))); perfmonFile.delete(); index++; } notifier.notifyAbout("Starting upload to Loadosophia.org"); String[] fields = multipartPost(partsList, getUploaderURI(), HttpStatus.SC_OK); int queueID = Integer.parseInt(fields[0]); results.setQueueID(queueID); if (!title.trim().isEmpty() || !colorFlag.equals(COLOR_NONE)) { int testID = getTestByUpload(queueID); results.setTestID(testID); if (!title.trim().isEmpty()) { setTestTitle(testID, title.trim()); } if (!colorFlag.equals(COLOR_NONE)) { setTestColor(testID, colorFlag); } results.setRedirectLink(address + "gui/" + testID + "/"); } else { results.setRedirectLink(address + "api/file/status/" + queueID + "/?redirect=true"); } return results; }
From source file:org.safecreative.api.RegisterWork.java
/** * Uploads a file/*from w ww . j av a2 s. c o m*/ * @param uploadURL * @param uploadID * @param file File to upload * @param checksum * @return uploadticket * @throws Exception */ public String uploadFile(String uploadURL, String uploadID, final File file, String checksum) throws Exception { return uploadFile(uploadURL, uploadID, new FilePartSource(file), checksum); }