大佬教程收集整理的这篇文章主要介绍了“Curl -F”Java等价物,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
curl -X POST -F "file=@$File_PATH"
我想用Java执行的请求是:
curl -X POST -F 'file=@file_path' http://localhost/files/
我在努力:
httpClient httpClient = new DefaulthttpClient(); httpPost httpPost = new httpPost(_URL); File file = new File(PATH); multipartentity mpEntity = new multipartentity(); ContentBody cbFile = new FileBody(file,"bin"); mpEntity.addPart("userfile",cbFilE); httpPost.setEntity(mpEntity); httpResponse response = httpClient.execute(httpPost); InputStream instream = response.getEntity().getContent();
package curldashf; import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; import org.apache.http.httpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.fluent.request; import org.apache.http.entity.mime.multipartentity; import org.apache.http.entity.mime.content.byteArrayBody; import org.apache.http.util.EntityUtils; public class CurlDashF { public static void main(String[] args) throws ClientProtocolException,IOException { String filePath = "file_path"; String url = "http://localhost/files"; File file = new File(filePath); multipartentity entity = new multipartentity(); entity.addPart("file",new FileBody(filE)); httpResponse returnResponse = request.Post(url) .body(entity) .execute().returnResponse(); System.out.println("Response status: " + returnResponse.getStatusLine().getStatusCode()); System.out.println(EntityUtils.toString(returnResponse.getEntity())); } }
根据需要设置filePath和url.如果您使用的是文件以外的其他内容,则可以使用ByteArrayBody,InputStreamBody或StringBody替换FileBody.我的特殊情况需要ByteArrayBody,但上面的代码适用于文件.
以上是大佬教程为你收集整理的“Curl -F”Java等价物全部内容,希望文章能够帮你解决“Curl -F”Java等价物所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。