這輩子沒辦法做太多事情,所以每一件都要做到精彩絕倫!
People can't do too many things in my life,so everything will be wonderful
項目使用技術SpringMVC + spring + mybatis
該工具類封裝了get、post、put、delete以及post上傳多個文件等方法;包含了有無參數。日常開發,夠用了!
一般來說,有些公司會有自己獨立的上傳下載文件服務器;有些是單應用服務!以上這兩種情況,暫用不到該方法。
本文重點講解使用httpClient進行post請求多文件上傳功能(底層是模擬表單提交)。
依賴包及版本
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.5</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.3.5</version>
</dependency>
方法代碼:
//注入httpClient和requestConfig(spring集成httpClient配置)
@Autowired
privateCloseableHttpClient httpClient;
@Autowired
privateRequestConfig requestConfig;
/**
*
* @描述:httpCilent多圖片上傳和多個參數
* @創建人:wyait
* @創建時間:2017年5月2日 下午1:47:41
* @param url 請求url
* @param params 請求參數
* @param files file對象
* @return
* @throws IOException
*/
publicHttpResult postUploadFile(String url, Map<String, Object> params,
Map<String,File> files) throws IOException {
HttpPosthttpPost = new HttpPost(url);// 創建 HTTP POST 請求
httpPost.setConfig(this.requestConfig);
MultipartEntityBuilderbuilder = MultipartEntityBuilder.create();
builder.setCharset(Charset.forName("UTF-8"));//設置請求的編碼格式
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);//設置瀏覽器兼容模式
//設置參數
if(files != null) {
//設置圖片參數
for(Map.Entry<String, File> entry : files.entrySet()) {
builder.addBinaryBody(entry.getKey(),entry.getValue());
}
}
//設置參數
if(params != null) {
//設置post參數
for(Map.Entry<String, Object> entry : params.entrySet()) {
//指定編碼,防止中文亂碼問題。但是某些情況下會導致上傳失敗
builder.addTextBody(entry.getKey(),
String.valueOf(entry.getValue()),
ContentType.create("text/plain","UTF-8"));
}
}
//生成 HTTP POST 實體
HttpEntityentity = builder.build();
//設置請求參數
httpPost.setEntity(entity);
CloseableHttpResponseresponse = null;
try{
//執行請求
response= httpClient.execute(httpPost);
if(response.getEntity() != null) {
returnnew HttpResult(response.getStatusLine().getStatusCode(),
EntityUtils.toString(response.getEntity(),"UTF-8"));
}
returnnew HttpResult(response.getStatusLine().getStatusCode(),
null);
}finally {
if(response != null) {
response.close();
}
}
}
現有A前臺系統和B服務系統;業務流程:
JSP頁面提交文件—>A系統(發送httpClient)—>B系統進行更新數據!
考慮的方案:
1,直接跳過A系統,jsp頁面請求B更新數據;
2,A系統進行文件保存后,將路徑帶到B系統進行更新操作;
以上兩種方案的問題點;
方案1,線上B服務,一般是內網服務,不對外開放;否則會有安全問題;
方案2,涉及分布式事務問題;
在網上也百度了很多方法,最終退而求其次,進行兩次讀寫操作,更新數據!
此處略
主要功能是:A系統接收文件,合法性校驗后,對圖片進行判斷和壓縮處理,生成一個臨時文件;
對于對圖片的判斷處理這里就不做過多說明了。
重點說一下:
MultipartFile 轉成 File對象實現(結合網上資料,總結出最佳實踐):
Stringpath=”自定義”+MultipartFile. getOriginalFilename();
File newFile=new File(path);
//直接寫文件到指定路徑下
MultipartFile.transferTo(newFile);
主要功能是:調用httpClient已經封裝好的postUploadFile(ur,params,files)方法,發送請求;
Map<String, File> files = new HashMap<String, File>();
files.put("newFile", newFile);
//發送請求,并對請求數據進行處理,params自定義
Obj 自定義=httpCilentService.postUploadFile(url, params, files);
B系統通過MultipartFile接收文件數據,并進行更新操作,返回結果;
接收文件:
保存文件:
// 新file
FilenewFile = new File(newFile);
//寫文件到磁盤
newPic.transferTo(newFile);
A系統在調用B系統后,無論結果ok,還是fail。都刪除臨時圖片;
將該段代碼寫在finally代碼塊中:
boolean flag = new File(path).delete();
if(!flag) {
//刪除原始圖片失敗
return"刪除臨時圖片失敗,請稍后再試";
}
最后,返回結果到jsp頁面
關于HttpClient工具類最好鏈接:http://blog.csdn.net/column/details/httpclient-arron.html
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。