项目媒体文件之前都是上传到七牛云处理,现在客户为了安全和私密性,准备将移动端拍摄的图片和视频传递到文件服务器,所以就想办法能不能在服务器端发布一个WebService,供移动端调用。刚好之前做的接口都是使用jersey架构来处理的,后来查了一下资料,刚好jersey提供了文件POST的处理。
客户端代码:
package env.ws.restful.media; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import org.apache.http.Consts; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; public class Test { public static void httpPost() { String url = "http://127.0.0.1:8001/rest/attachment/fileload"; //服务端发布的接口地址 String filePath = "D:/123.jpg"; //需要传递到服务器的文件路径 File file = new File(filePath); try { InputStream in = new FileInputStream(file); MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create(); entityBuilder.addTextBody("type", "image"); entityBuilder.addTextBody("filename", "001.jpg"); System.out.println("Client:"+in.available()); entityBuilder.addBinaryBody("file", in, ContentType.MULTIPART_FORM_DATA, "234.jpg"); // entityBuilder.addBinaryBody("file", in); CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url); httpPost.setEntity(entityBuilder.build()); //执行post接口 HttpResponse response = httpClient.execute(httpPost); String result=""; if (response!=null && response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { HttpEntity resEntity = response.getEntity(); if(resEntity != null){ result = EntityUtils.toString(resEntity,Consts.UTF_8); } } System.out.println(result); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { httpPost(); } }
服务器端代码:
package env.ws.restful.media; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import javax.ws.rs.Consumes; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import org.json.JSONObject; import com.sun.jersey.core.header.FormDataContentDisposition; import com.sun.jersey.multipart.FormDataParam; import p2.ws.restful.WSRESTService; @Path("/attachment") public class AttchmentService implements WSRESTService{ public AttchmentService() { } @POST @Path("/fileload") @Produces({MediaType.APPLICATION_JSON}) @Consumes(MediaType.MULTIPART_FORM_DATA) public String upLoadFile(@FormDataParam("file") InputStream fileInputStream, @FormDataParam("file") FormDataContentDisposition disposition, @FormDataParam("type") String type, @FormDataParam("filename") String resourceId) { System.out.println("开始调用!"); JSONObject result = new JSONObject(); if(type==null || resourceId==null || fileInputStream==null){ result.put("code", "10001"); result.put("msg", "参数不正确"); return result.toString(); } FileOutputStream out=null; try { out = new FileOutputStream(new File("D:/Test/"+disposition.getFileName())); byte[] bytes = new byte[1024]; int byteCount = 0; while ((byteCount = fileInputStream.read(bytes)) != -1){ out.write(bytes, 0, byteCount); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ try { fileInputStream.close(); out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); } } System.out.println("结束调用!"); result.put("code", "10000"); result.put("msg", "上传成功!"); return result.toString(); } }
执行客户端Test.java,调用成功以后会在服务器端的 D:\Test 盘下生成234.jpg