亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用RestTemplate請求將多部分文件作為POST參數發送

使用RestTemplate請求將多部分文件作為POST參數發送

呼啦一陣風 2019-11-29 10:33:07
我正在使用Spring 3和RestTemplate。我基本上有兩個應用程序,其中一個必須將值發布到另一個應用程序。通過休息模板。當要發布的值是String時,它是完美的工作,但是當我必須發布混合和復雜的參數(例如MultipartFiles)時,我會收到轉換器異常。例如,我有這個:App1- PostController:@RequestMapping(method = RequestMethod.POST)public String processSubmit(@ModelAttribute UploadDTO pUploadDTO,         BindingResult pResult) throws URISyntaxException, IOException {    URI uri = new URI("http://localhost:8080/app2/file/receiver");    MultiValueMap<String, Object> mvm = new LinkedMultiValueMap<String, Object>();    mvm.add("param1", "TestParameter");    mvm.add("file", pUploadDTO.getFile()); // MultipartFile    Map result = restTemplate.postForObject(uri, mvm, Map.class);    return "redirect:postupload";}另一方面...我有另一個Web應用程序(App2),它從App1接收參數。App2- ReceiverController@RequestMapping(value = "/receiver", method = { RequestMethod.POST })public String processUploadFile(        @RequestParam(value = "param1") String param1,        @RequestParam(value = "file") MultipartFile file) {    if (file == null) {        System.out.println("Shit!... is null");    } else {        System.out.println("Yes!... work done!");    }    return "redirect:postupload";}我的application-context.xml:<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">    <property name="messageConverters">        <list>            <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />            <bean class="org.springframework.http.converter.FormHttpMessageConverter" />            <bean class="org.springframework.http.converter.StringHttpMessageConverter" />            <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter" />        </list>    </property></bean>所以我的問題是:是否可以使用POST通過RestTemplate發送MultipartFile?我必須使用某些特定的轉換器來發送此類對象嗎?我的意思是在配置中要使用一些MultipartFileHttpMessageConverter嗎?
查看完整描述

3 回答

?
牛魔王的故事

TA貢獻1830條經驗 獲得超3個贊

解決此問題而無需使用需要磁盤上文件的FileSystemResource的一種方法是使用ByteArrayResource,這樣您就可以在帖子中發送字節數組(此代碼在Spring 3.2.3中有效):


MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();

final String filename="somefile.txt";

map.add("name", filename);

map.add("filename", filename);

ByteArrayResource contentsAsResource = new ByteArrayResource(content.getBytes("UTF-8")){

            @Override

            public String getFilename(){

                return filename;

            }

        };

map.add("file", contentsAsResource);

String result = restTemplate.postForObject(urlForFacade, map, String.class);

我重寫ByteArrayResource的getFilename,因為如果我不得到空指針異常(顯然,這取決于java激活.jar是否在類路徑中,如果存在,它將使用文件名來嘗試確定內容類型)


查看完整回答
反對 回復 2019-11-29
?
LEATH

TA貢獻1936條經驗 獲得超7個贊

前幾天,我也遇到了同樣的問題。Google搜索將我帶到了這里和其他幾個地方,但沒有一個提供解決此問題的方法。我最終將上傳的文件(MultiPartFile)保存為tmp文件,然后使用FileSystemResource通過RestTemplate上傳它。這是我使用的代碼


String tempFileName = "/tmp/" + multiFile.getOriginalFileName();

FileOutputStream fo = new FileOutputStream(tempFileName);


fo.write(asset.getBytes());    

fo.close();   


parts.add("file", new FileSystemResource(tempFileName));    

String response = restTemplate.postForObject(uploadUrl, parts, String.class, authToken, path);   



//clean-up    

File f = new File(tempFileName);    

f.delete();

我仍在尋找解決此問題的更優雅的方法。


查看完整回答
反對 回復 2019-11-29
  • 3 回答
  • 0 關注
  • 2782 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號