使用OkHTTP来调用restful接口上传文件
来源:互联网 发布:天猫双11晚会网络直播 编辑:程序博客网 时间:2024/05/22 05:05
首先想要了解okhttp相关功能,请查看文章:http://blog.csdn.net/win7system/article/details/51697884,
闲话就不多说了,直接来干货
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 { /** * The imgur client ID for OkHttp recipes. If you're using imgur for anything * other than running these examples, please request your own client ID! * https://api.imgur.com/oauth2 */ 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 { // Use the imgur image upload API as documented at https://api.imgur.com/endpoints/image 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()); } }</span>
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>
1 0
- 使用OkHTTP来调用restful接口上传文件
- 关于使用RESTful api上传文件,基于jax rs接口,不是实现
- OkHttp使用(三)文件上传
- OkHttp使用(三)文件上传
- OkHttp使用+文件的上传+断点续传
- 使用OKHttp上传、下载文件记录进度
- restful接口的调用
- 使用okhttp上传文件---单文件或多文件
- 使用SpringSecurity和SpringMVC来实现安全的RESTFul接口
- 使用token和SpringMVC来实现安全的RESTFul接口
- okhttp上传文件实例
- okhttp上传文件
- okhttp-上传文件
- okhttp封装文件上传
- okhttp上传文件
- OKhttp post上传文件
- 使用Okhttp上传图片
- 使用OkHttp上传图片
- iOS UIAlertController上修改标题(title) 消息(message) 按钮 的字体大小及颜色
- PHP调用JS验证弹出框无法显示的问题
- test dword ptr [eax],eax ; probe page.
- 高德地图V3.3.2在非arm64-v8a,armeabi的CPU架构手机上运行奔溃的问题
- JMF开启摄像头
- 使用OkHTTP来调用restful接口上传文件
- IntelliJ IDEA WEB项目的部署配置
- 麒麟开源堡垒机安装部署测试及优缺点总结
- C++归并排序代码
- 【bzoj1398】Vijos1382寻找主人 Necklace
- xshell连接linux中文编码问题
- Android传感器(一):传感器列表
- android listview系列之初步接触listview(一)
- addChildViewController