jersey檔案上傳

來源:互聯網
上載者:User

標籤:jersey

最近在搞webService的檔案上傳。伺服器端好搞,就是用戶端比較麻煩,mark一下。

服務端的代碼:

@POST@Path("uploadFile")@Consumes(MediaType.MULTIPART_FORM_DATA)@Produces(MediaType.APPLICATION_XML)public String uploadFile(FormDataMultiPart form) {    String filePath;    String baseUrl = AppConfig.getProperty("res.root.path");    try {        UploadRule rule = new UploadRule(baseUrl,Constants.SPT + UploadRule.ACCESS_SCOPE_PROTECTED + SCOPE);        //boay中可能有多個附件,迴圈出來        List<BodyPart> v1 = form.getBodyParts();        List<String> paths = new ArrayList<String>();        for (BodyPart p : v1) {            FormDataBodyPart vPart = (FormDataBodyPart) p;            InputStream v = vPart.getValueAs(InputStream.class);            String fileName = vPart.getName();            File destDir = new File("d:/upload/files");            if(!destDir.exists())                 destDir.mkdirs();             OutputStream outputStream = new FileOutputStream(new File("d:/upload/files/"+fileName ));            int length = 0;            byte[] buff = new byte[1024];            while (-1 != (length = v.read(buff))) {                outputStream.write(buff, 0, length);            }            v.close();            outputStream.close();<pre name="code" class="html">            filePath = "d:/upload/files/"+fileName 
        }
        response.setPaths(paths);
        return filePath;
    } catch (Exception e) {
        e.printStackTrace();
        response.setResponseStatus(BaseResponse.RESPONSE_STATUS_ERROR);
        response.setResponseMessage(BaseUtil.getStackTrace(e));
        return filePath;  
 }
}

用戶端代碼:
 

public static void main(String[] args) {ClientConfig config = new DefaultClientConfig();Client client = Client.create(config);//上傳的地址WebResource r = client.resource("http://jjy.cost168.com/api/upload/uploadFile");FormDataMultiPart form=new FormDataMultiPart();File f = new File("d:/x1.txt");InputStream file = null;try {file = new FileInputStream(f);} catch (FileNotFoundException e) {e.printStackTrace();}FormDataBodyPart formDataBodyPart = new FormDataBodyPart();//這裡指定上傳時的類型formDataBodyPart.setValue(MediaType.MULTIPART_FORM_DATA_TYPE, file);formDataBodyPart.setName("x1.txt");form.getBodyParts().add(formDataBodyPart);//這樣寫的好處是可以上傳多個附件//這裡的type也要指定為dataFilesResponse files = r.accept(MediaType.APPLICATION_XML).type(MediaType.MULTIPART_FORM_DATA).post(FilesResponse.class, form);for (String string : files.getPaths()) {System.out.println(string);}}
需要用到的jar

<dependency><groupId>com.cost168.lib.bl</groupId><artifactId>rest_core</artifactId><version>0.0.1-SNAPSHOT</version></dependency><dependency><groupId>com.sun.jersey</groupId><artifactId>jersey-core</artifactId><version>1.17.1</version></dependency><dependency><groupId>com.sun.jersey</groupId><artifactId>jersey-client</artifactId><version>1.17.1</version></dependency><dependency><groupId>com.sun.jersey</groupId><artifactId>jersey-json</artifactId><version>1.17.1</version></dependency><dependency><groupId>com.sun.jersey.contribs</groupId><artifactId>jersey-multipart</artifactId><version>1.17.1</version></dependency>













jersey檔案上傳

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.