項目媒體文件之前都是上傳到七牛雲處理,現在客戶為了安全和私密性,准備將移動端拍攝的圖片和視頻傳遞到文件服務器,所以就想辦法能不能在服務器端發布一個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