RESTEasy使用httpclient上传文件
来源:互联网 发布:大疆飞控算法 编辑:程序博客网 时间:2024/06/05 06:30
我们使用resteasy-multipart的MultipartFormDataInput类来操作数据。
1) 更新maven项目依赖
添加下面的maven依赖到你的项目:
<!-- core library --> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-jaxrs</artifactId> <version>2.3.1.GA</version> </dependency> <dependency> <groupId>net.sf.scannotation</groupId> <artifactId>scannotation</artifactId> <version>1.0.2</version> </dependency> <!-- JAXB provider --> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-jaxb-provider</artifactId> <version>2.3.1.GA</version> </dependency> <!-- Multipart support --> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-multipart-provider</artifactId> <version>2.3.1.GA</version> </dependency> <!-- For better I/O control --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.0.1</version> </dependency>
同样也添加以下图片中的jar包,到你的项目依赖中:
2) 准备http client,用于客户端上传文件:
package com.howtodoinjava.client.upload; import java.io.File; import org.apache.http.HttpResponse;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpPost;import org.apache.http.entity.mime.MultipartEntity;import org.apache.http.entity.mime.content.FileBody;import org.apache.http.entity.mime.content.StringBody;import org.apache.http.impl.client.DefaultHttpClient; public class DemoFileUploader { public static void main(String args[]) throws Exception { DemoFileUploader fileUpload = new DemoFileUploader () ; File file = new File("C:/Lokesh/Setup/workspace/RESTfulDemoApplication/target/classes/Tulips.jpg") ; //Upload the file fileUpload.executeMultiPartRequest("http://localhost:8080/RESTfulDemoApplication/user-management/image-upload", file, file.getName(), "File Uploaded :: Tulips.jpg") ; } public void executeMultiPartRequest(String urlString, File file, String fileName, String fileDescription) throws Exception { HttpClient client = new DefaultHttpClient() ; HttpPost postRequest = new HttpPost (urlString) ; try { //Set various attributes MultipartEntity multiPartEntity = new MultipartEntity () ; multiPartEntity.addPart("fileDescription", new StringBody(fileDescription != null ? fileDescription : "")) ; multiPartEntity.addPart("fileName", new StringBody(fileName != null ? fileName : file.getName())) ; FileBody fileBody = new FileBody(file, "application/octect-stream") ; //Prepare payload multiPartEntity.addPart("attachment", fileBody) ; //Set to request body postRequest.setEntity(multiPartEntity) ; //Send request HttpResponse response = client.execute(postRequest) ; //Verify response if any if (response != null) { System.out.println(response.getStatusLine().getStatusCode()); } } catch (Exception ex) { ex.printStackTrace() ; } }}
3)编写RESTful API应用来处理文件请求:
package com.howtodoinjava.client.upload; import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.util.List;import java.util.Map; import javax.ws.rs.Consumes;import javax.ws.rs.POST;import javax.ws.rs.Path;import javax.ws.rs.core.MultivaluedMap;import javax.ws.rs.core.Response; import org.apache.commons.io.IOUtils;import org.jboss.resteasy.plugins.providers.multipart.InputPart;import org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput; @Path("/user-management")public class DemoFileSaver_MultipartFormDataInput { private final String UPLOADED_FILE_PATH = "c:\temp\"; @POST @Path("/image-upload") @Consumes("multipart/form-data") public Response uploadFile(MultipartFormDataInput input) throws IOException { //Get API input data Map<String, List<InputPart>> uploadForm = input.getFormDataMap(); //Get file name String fileName = uploadForm.get("fileName").get(0).getBodyAsString(); //Get file data to save List<InputPart> inputParts = uploadForm.get("attachment"); for (InputPart inputPart : inputParts) { try { //Use this header for extra processing if required @SuppressWarnings("unused") MultivaluedMap<String, String> header = inputPart.getHeaders(); // convert the uploaded file to inputstream InputStream inputStream = inputPart.getBody(InputStream.class, null); byte[] bytes = IOUtils.toByteArray(inputStream); // constructs upload file path fileName = UPLOADED_FILE_PATH + fileName; writeFile(bytes, fileName); System.out.println("Success !!!!!"); } catch (Exception e) { e.printStackTrace(); } } return Response.status(200) .entity("Uploaded file name : "+ fileName).build(); } //Utility method private void writeFile(byte[] content, String filename) throws IOException { File file = new File(filename); if (!file.exists()) { file.createNewFile(); } FileOutputStream fop = new FileOutputStream(file); fop.write(content); fop.flush(); fop.close(); }}
Happy Learning !!
0 0
- RESTEasy使用httpclient上传文件
- RESTEasy文件上传,使用html form表格
- resteasy 上传文件
- 使用httpclient上传文件
- 使用httpclient上传文件
- 使用HttpClient上传文件
- 使用commons-httpclient上传文件
- 使用socket、httpclient 文件上传
- resteasy上传文件中文乱码解决
- Dubbox RestEasy 文件上传乱码解决
- 使用httpclient上传文件到web服务器
- 使用 HttpClient 4 进行文件上传
- 使用HttpClient上传文件资源(已验证)
- 使用HttpClient实现百度PCS文件上传
- 使用 HttpClient 4 进行文件上传
- SpringMVC使用HttpClient实现文件上传
- 使用HttpClient实现文件的上传
- 使用httpclient提交表单方式上传文件
- poj 1837 DP
- test
- Protel中的元器件旋转
- Vim多文件搜索特定内容
- 回调函数
- RESTEasy使用httpclient上传文件
- mysql_free_result&bool mysqli_close
- ecshop模版设置添加广告不成功
- 最短路
- Udacity cs344-Introduction to Parallel Programming学习笔记-第三单元
- [Java基础] Annotation详解
- lua面向对象实现-类实例化对象、继承、多态、多继承、lua单例模式
- ios--常用元素控件尺寸
- 解决 Ubuntu 12.04 无法调节屏幕亮度的问题