List of usage examples for org.apache.commons.httpclient.methods.multipart PartSource getLength
public abstract long getLength();
From source file:org.safecreative.api.RegisterWork.java
/** * Post data using an instance of <code>PartSource</code> * @param uploadUrl//from w ww .j a va 2 s .c o m * @param params * @param file <code>PartSource</code> to upload * @return uploadticket */ public String postFile(String uploadUrl, Map<String, String> params, final PartSource file) { PostMethod post = null; try { log.debug(String.format("api request by post: \n%s/%s\n", uploadUrl, file.getFileName())); if (client == null) { client = new HttpClient(); } post = new PostMethod(uploadUrl); Part[] parts = new Part[params.size() + 1]; int i = 0; for (Map.Entry<String, String> param : params.entrySet()) { parts[i++] = new StringPart(param.getKey(), param.getValue(), SafeCreativeAPI.DEFAULT_ENCODING); } parts[i] = new FilePart("file", file, "application/octet-stream", SafeCreativeAPI.DEFAULT_ENCODING) { @Override protected void sendData(OutputStream out) throws IOException { if (uploadProgressListener == null) { super.sendData(out); } else { super.sendData(new PostUploadProgressListenerOutputStream(out, uploadProgressListener, file.getLength())); } } }; MultipartRequestEntity entity = new MultipartRequestEntity(parts, post.getParams()); post.setRequestEntity(entity); int status = client.executeMethod(post); if (status != HttpURLConnection.HTTP_OK) { throw new IOException("Response error " + status); } return post.getResponseBodyAsString(); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new RuntimeException(e); } finally { if (post != null) { post.releaseConnection(); } } }
From source file:org.safecreative.api.RegisterWork.java
/** * Uploads a file//from w w w . j a va 2s . c o m * @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"); }