使用OkHTTP来调用restful接口上传文件
2016-06-17 13:57
513 查看
首先想要了解okhttp相关功能,请查看文章:http://blog.csdn.net/win7system/article/details/51697884,
闲话就不多说了,直接来干货
okhttp接口
restful接口
另外需要在web.xml下添加一个初始化参数才能起作用
如果是maven项目还需要导入如下包
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.22.2</version>
</dependency>
闲话就不多说了,直接来干货
okhttp接口
import java.io.File; import java.io.IOException; import com.squareup.okhttp.MediaType; import com.squareup.okhttp.MultipartBuilder; import com.squareup.okhttp.OkHttpClient; import com.squareup.okhttp.Request; import com.squareup.okhttp.RequestBody; import com.squareup.okhttp.Response; public class PostMultipart { private static final String IMGUR_CLIENT_ID = "9199fdef135c122"; private static final MediaType MEDIA_TYPE_DATA = MediaType.parse("multipart/form-data"); private final OkHttpClient client = new OkHttpClient(); public void run(String url, String filePath) throws Exception { File file = new File(filePath); RequestBody requestBody = new MultipartBuilder() .type(MultipartBuilder.FORM) .addFormDataPart("filename", file.getName()) .addFormDataPart("file", file.getName(), RequestBody.create(MEDIA_TYPE_DATA, file)) .build(); Request request = new Request.Builder() .header("Authorization", "Client-ID " + IMGUR_CLIENT_ID) .url(url) .post(requestBody) .build(); Response response = client.newCall(request).execute(); if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); System.out.println(response.body().string()); } }
restful接口
@POST @Path("/upload") @Consumes(MediaType.MULTIPART_FORM_DATA) @Produces(MediaType.APPLICATION_JSON) public Response uploadFile(@FormDataParam("file") InputStream uploadedInputStream, @FormDataParam("file") FormDataContentDisposition fileDetail, @FormDataParam("filename") String filename) throws IOException { System.out.println("文件创建时间:" + fileDetail.getFileName()); System.out.println("文件名字:" + filename); File file = new File("D:\\demo\\"+fileDetail.getFileName()); if (!file.exists()) file.createNewFile(); FileOutputStream out = new FileOutputStream(file); int c; byte buffer[] = new byte[1024]; while ((c = uploadedInputStream.read(buffer)) != -1) { for (int i = 0; i < c; i++) out.write(buffer[i]); } Map<String, Object> map = new HashMap<String, Object>(); map.put("文件读取日期", fileDetail.getReadDate()); map.put("文件大小", fileDetail.getSize()); map.put("文件类型", fileDetail.getType()); map.put("文件读取", uploadedInputStream.read()); uploadedInputStream.close(); out.close(); return Response.status(200).entity(map).build(); }
另外需要在web.xml下添加一个初始化参数才能起作用
<init-param> <param-name>jersey.config.server.provider.classnames</param-name> <param-value>org.glassfish.jersey.filter.LoggingFilter;org.glassfish.jersey.media.multipart.MultiPartFeature</param-value> </init-param>
org.glassfish.jersey.filter.LoggingFilter是可以打印出jersey上传文件时候的日志,可以选择也可以不用选择
如果是maven项目还需要导入如下包
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.22.2</version>
</dependency>
相关文章推荐
- HttpClient,HttpUrlConnection的区别
- Android AsyncHttpClient 下载文件 Content-Type not allowed! 错误
- 浅谈HTTP中Get与Post的区别
- 11、查看及测试网络、配置网络地址、内核及模块管理
- 学院引我走向新世界
- cocoaPods使用
- android 网络请求总结
- 获得网络接口信息和网卡信息
- HTTPS 协议和原理
- java 使用socket5/http代理做http访问
- 下载普通http连接的图片和ftp服务器的图片
- uhttpd配置文件分析
- HTTP 错误 500.21 - Internal Server Error以及aspnet_regiis.exe 不是有效的 Win32 应用程序的解决方法
- Python 标准库 —— socket(网络编程)
- apache httpclient-4.5 https通讯 双向认证
- android httpClient 支持HTTPS的2种处理方式
- tomcat配置https访问
- apache启用gzip压缩方法--转载自http://www.cnblogs.com/linzhenjie/archive/2013/03/05/2943635.html
- HTTP/2在一个TCP连接里以frame封装的数据包方式多工传输。
- http中的keep-alive