List of usage examples for org.apache.commons.httpclient.methods.multipart PartSource createInputStream
public abstract InputStream createInputStream() throws IOException;
From source file:com.boyuanitsm.pay.alipay.util.AlipayCore.java
/** * ??// w w w. ja v a 2s . 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:ome.formats.importer.util.ErrorFilePart.java
protected void sendData(OutputStream out) throws IOException { // Content slurped and modified from superclass. // Chris Allan <callan@glencoesoftware.com> PartSource source = getSource(); byte[] tmp = new byte[4096]; InputStream instream = source.createInputStream(); try {/*from w ww . j a v a 2 s . com*/ int rlen; while ((rlen = instream.read(tmp)) >= 0) { if (cancel) { //System.err.println("cancelled"); break; } out.write(tmp, 0, rlen); //System.err.println("ding"); } } finally { // we're done with the stream, close it instream.close(); } }
From source file:org.openmicroscopy.shoola.svc.proxy.SubmittedFilePart.java
/** * Overridden to read chunk of the file. * @see FilePart#sendData(OutputStream)/*from w w w. j a v a 2 s . c o m*/ */ protected void sendData(OutputStream out) throws IOException { PartSource source = getSource(); byte[] tmp = new byte[SIZE]; InputStream in = source.createInputStream(); try { int l; while ((l = in.read(tmp)) >= 0) out.write(tmp, 0, l); } finally { in.close(); } }
From source file:org.safecreative.api.RegisterWork.java
/** * Uploads a file//from www .j av a2 s . c om * @param uploadURL * @param uploadID * @param file <code>PartSource</code> to upload * @param checksum * @return uploadticket * @throws Exception */ public String uploadFile(String uploadURL, String uploadID, final PartSource file, String checksum) throws Exception { String response; Map<String, String> params; api.setBaseUrl(uploadURL); params = api.createParams("component", "work.upload.begin"); params.put("authkey", api.getAuthKey()); params.put("uploadid", uploadID); response = api.callSigned(params, api.getPrivateAuthKey(), true, false); checkError(params, response); log.debug("response {}", response); String state = api.getResponseState(response); if (!"ready".equalsIgnoreCase(state)) { throw new RuntimeException("Unexpected work.upload.begin state " + state); } //////////////////////////////////////////////////////////////////// //Chunk: int chunkSize = 50 * 1024; long offset = 0; int readed = 0; int percent = 0; byte[] chunkBuffer = new byte[chunkSize]; byte[] data; long uploadSize = file.getLength(); InputStream is = null; if (uploadProgressListener != null) { uploadProgressListener.uploadProgress(percent, offset, uploadSize); } try { is = file.createInputStream(); while (offset < uploadSize) { readed = is.read(chunkBuffer); if (readed <= 0) { break; } data = new byte[readed]; System.arraycopy(chunkBuffer, 0, data, 0, readed); params = api.createParams("component", "work.upload.chunk"); params.put("authkey", api.getAuthKey()); params.put("uploadid", uploadID); params.put("offset", String.valueOf(offset)); String encoded = Base64.encodeBytes(data); params.put("data", encoded); response = api.callSigned(params, api.getPrivateAuthKey(), true, false); checkError(params, response); state = api.getResponseState("workuploadchunk", response); if (!"continue".equalsIgnoreCase(state)) { throw new RuntimeException("Unexpected work.upload.chunk state " + state); } offset += readed; percent = (int) ((100f * (float) offset) / (float) uploadSize); log.debug("CHUNK UPLOADED {}%", percent); if (uploadProgressListener != null) { uploadProgressListener.uploadProgress(percent, offset, uploadSize); } } } finally { IOHelper.closeQuietly(is); } log.info("Work uploaded successfully!"); if (uploadProgressListener != null) { uploadProgressListener.uploadProgress(100, uploadSize, uploadSize); } //////////////////////////////////////////////////////////////////// //Commit: log.info("COMMIT"); params = api.createParams("component", "work.upload.commit"); params.put("authkey", api.getAuthKey()); params.put("uploadid", uploadID); params.put("length", String.valueOf(uploadSize)); params.put("checksum", checksum); response = api.callSigned(params, api.getPrivateAuthKey(), true, false); checkError(params, response); return api.evalXml(response, "/workuploadcommit/uploadticket"); }
From source file:org.safecreative.api.wrapper.SafeCreativeAPIWrapper.java
/** * Uploads a file part source for register or update a work * //from w w w.j a v a 2s . co m * @param fileSource <code>PartSource</code> to upload * @param byPost true - upload file using POST, false - upload by API * @param uploadListener Upload progress listener to notify if not <code>null</code>* * @param checkSum Optional SHA-1 checksum if available, otherwise it will be calculated again for the file part source * @return upload ticket or null if fails * @throws ApiException */ public String uploadFileSource(PartSource fileSource, boolean byPost, UploadProgressListener uploadListener, String... checkSum) throws ApiException { if (fileSource == null) { return null; } else { // Look up Map<String, String> params = api.createParams("component", "work.upload.lookup"); params.put("authkey", authKey.getAuthkey()); params.put("filename", fileSource.getFileName()); if (byPost) { params.put("bypost", "true"); } String response = api.callSigned(params, authKey.getPrivatekey(), true, false); checkError(response); String uploadURL = api.evalXml(response, "/workuploadlookup/uploadurl"); log.debug("Upload URL: {}", uploadURL); String uploadID = api.evalXml(response, "/workuploadlookup/uploadid"); log.debug("Upload id: {}", uploadID); // Upload String uploadTicket = null; RegisterWork registerer = new RegisterWork(api); registerer.setUploadProgressListener(uploadListener); api.setBaseUrl(uploadURL); if (byPost) { //////////////////////////////////////////////////////////////////// //POST Upload: log.info("registerWork upload by post file: {}", fileSource.getFileName()); params = api.createParams("uploadid", uploadID); uploadTicket = registerer.postFile(uploadURL, params, fileSource); log.info("Successfully uploaded file: {}", fileSource); } else { //////////////////////////////////////////////////////////////////// //API Upload: try { log.info("registerWork upload by API file: {}", fileSource.getFileName()); String sha1CheckSum = null; if (checkSum != null && checkSum.length == 1) { sha1CheckSum = checkSum[0]; } if (StringUtils.isBlank(sha1CheckSum)) { log.info("Building SHA-1 checksum"); sha1CheckSum = Digest.getHexDigest(fileSource.createInputStream(), Digest.SHA1); } uploadTicket = registerer.uploadFile(uploadURL, uploadID, fileSource, sha1CheckSum); log.info("Successfully uploaded file: {}", fileSource.getFileName()); } catch (Exception ex) { throw ApiException.wrap(ex); } } log.debug("uploadTicket {}", uploadTicket); return uploadTicket; } }