/** * 生成文件摘要 * @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 ""; } }
/** * 生成文件摘要 * * @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 ""; } }
private List<Part> buildParts(BaseRequest request) { List<Part> parts = new ArrayList<Part>(); for (PartHolder h : TankHttpUtil.getPartsFromBody(request)) { if (h.getFileName() == null) { StringPart stringPart = new StringPart(h.getPartName(), new String(h.getBodyAsString())); if (h.isContentTypeSet()) { stringPart.setContentType(h.getContentType()); } parts.add(stringPart); } else { PartSource partSource = new ByteArrayPartSource(h.getFileName(), h.getBody()); FilePart p = new FilePart(h.getPartName(), partSource); if (h.isContentTypeSet()) { p.setContentType(h.getContentType()); } parts.add(p); } } return parts; }
public static int sendFile(final String host, final String port, final String path, final String fileName, final InputStream inputStream, final long lengthInBytes) { HttpClient client = new HttpClient(); try { client.getParams().setParameter("http.protocol.version", HttpVersion.HTTP_1_1); client.getParams().setSoTimeout(3600 * 1000); // One hour PostMethod post = new PostMethod("http://" + host + ":" + port + "/" + path); Part[] parts = { new FilePart(fileName, new PartSource() { @Override public long getLength() { return lengthInBytes; } @Override public String getFileName() { return "fileName"; } @Override public InputStream createInputStream() throws IOException { return new BufferedInputStream(inputStream); } }) }; post.setRequestEntity(new MultipartRequestEntity(parts, new HttpMethodParams())); client.executeMethod(post); if (post.getStatusCode() >= 400) { String errorString = "POST Status Code: " + post.getStatusCode() + "\n"; if (post.getResponseHeader("Error") != null) { errorString += "ServletException: " + post.getResponseHeader("Error").getValue(); } throw new HttpException(errorString); } return post.getStatusCode(); } catch (Exception e) { LOGGER.error("Caught exception while sending file", e); Utils.rethrowException(e); throw new AssertionError("Should not reach this"); } }
/** * 生成文件摘要 * * @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)); switch (file_digest_type) { case "MD5": return DigestUtils.md5Hex(file.createInputStream()); case "SHA": return DigestUtils.sha256Hex(file.createInputStream()); default: return ""; } }
private PostMethod getPostMultiPartWithFiles(PostMethod method, KalturaParams kparams, KalturaFiles kfiles) { String boundary = "---------------------------" + System.currentTimeMillis(); List <Part> parts = new ArrayList<Part>(); parts.add(new StringPart (HttpMethodParams.MULTIPART_BOUNDARY, boundary)); for(Entry<String, String> itr : kparams.entrySet()) { parts.add(new StringPart (itr.getKey(), itr.getValue())); } for (String key : kfiles.keySet()) { final KalturaFile kFile = kfiles.get(key); parts.add(new StringPart (key, "filename="+kFile.getName())); if (kFile.getFile() != null) { // use the file File file = kFile.getFile(); try { parts.add(new FilePart(key, file)); } catch (FileNotFoundException e) { // TODO this sort of leaves the submission in a weird state... -AZ logger.error("Exception while iterating over kfiles", e); } } else { // use the input stream PartSource fisPS = new PartSource() { public long getLength() { return kFile.getSize(); } public String getFileName() { return kFile.getName(); } public InputStream createInputStream() throws IOException { return kFile.getInputStream(); } }; parts.add(new FilePart(key, fisPS)); } } Part allParts[] = new Part[parts.size()]; allParts = parts.toArray(allParts); method.setRequestEntity(new MultipartRequestEntity(allParts, method.getParams())); return method; }
/** * ContentTypeFilePart constructor. * * @param name the name of the part * @param partSource the source for this part * @param charset the charset encoding for this part. */ public ContentTypeFilePart(String name, PartSource partSource, String charset) { super(name, partSource, ContentType.get(partSource.getFileName()), charset); }
/** * ContentTypeFilePart constructor. * * @param name the name of the part * @param partSource the source for this part */ public ContentTypeFilePart(String name, PartSource partSource) { this(name, partSource, null); }