java從伺服器上傳本地圖片到圖片伺服器
阿新 • • 發佈:2019-02-02
原文地址
http://blog.csdn.net/xingfeichen/article/details/54378790
1,本地上傳程式碼
- /**
- * 圖片上傳
- * </p>
- * @param avatarImgPath
- * @return
- */
- private String uploadImgToServer(String avatarImgPath) {
- MultipartEntityBuilder builders = MultipartEntityBuilder.create();
-
FileBody imgage = new FileBody(
- builders.addPart("img1", imgage);
- HttpEntity reqEntitys = builders.build();
- String results = HttpClientPostUtils
- .postMultiPart("http://192.168.0.221:8080/uploadweb/imgUpload/taskExecuteImg", reqEntitys);
- return results;
- }
-
publicstatic String postMultiPart(String url,HttpEntity reqEntity){
- CloseableHttpClient httpclient = HttpClients.createDefault();
- try{
- HttpPost method = new HttpPost(url);
- // MultipartEntity reqEntity = new MultipartEntity();
- method.setEntity(reqEntity);
-
CloseableHttpResponse response = httpclient.execute(method);
- try{
- //HttpResponse response=httpClient.execute(method);
- int statusCode = response.getStatusLine().getStatusCode();
- if(statusCode == HttpStatus.SC_OK){
- String body=EntityUtils.toString(response.getEntity());
- return body;
- }}finally{
- response.close();
- }
- }catch(Exception e){
- e.printStackTrace();
- }finally{
- try {
- httpclient.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- returnnull;
- }
- /**
- * 儲存圖片到本地
- * </p>
- * @param isDefaultSuffix
- * 是否使用預設字尾名
- * @param path
- * 圖片相對路徑
- * @param type
- * 圖片型別
- * @param request
- * @param response
- * @return
- */
- private String saveImgByType(boolean isDefaultSuffix, String path, String type, MultipartHttpServletRequest request,
- HttpServletResponse response) {
- // if (this.isUploadBlank(request)) {
- // logger.error("upload" + type + " img is null------上傳檔案為空");
- // return "";
- // }
- logger.info("upload" + type + " img start path: " + path);
- Iterator<String> itr = request.getFileNames();
- MultipartFile mpf = null;
- while (itr.hasNext()) {
- try {
- mpf = request.getFile(itr.next());
- if(mpf.isEmpty()){
- logger.error("upload" + type + " img is null------上傳檔案為空");
- returnnull;
- }
- //
- int imgSzie = mpf.getInputStream().available();
- // 獲取檔名
- String fileName = mpf.getOriginalFilename();
- if (StringUtils.isBlank(fileName)) {
- logger.error("檔名為空");
- returnnull;
- }
- // 檔案字尾
- String suffix = ".png";
- if(!isDefaultSuffix){
- suffix = fileName.substring(fileName.lastIndexOf("."));
- }
- if (mpf.getSize() / 1000 <= 30000 && mpf.getSize() == imgSzie) {
- // 圖片相對路徑
- String imgPath = path;
- if (StringUtils.isBlank(imgPath)) {
- imgPath = "files" + File.separator + getImagePath(type) + suffix; // 獲得目錄下的相對路徑
- }
- // 圖片絕對路徑
- String originalName = MyFileUtil.FILE_PATH + File.separator + imgPath;
- // 儲存圖片
- MyFileUtil.saveInputStreamToFile(mpf.getInputStream(), originalName);
- // 只針對任務和任務執行的圖片進行壓縮
- if (originalName.contains(File.separator + IMG_TYPE_TASK + File.separator)
- || originalName.contains(File.separator + IMG_TYPE_TASKEXE + File.separator)) {
- // 獲取縮圖路徑
- String smallImgPath = getSmallImgName(originalName);
- if (StringUtils.isNotBlank(smallImgPath)) {
- // 臨時儲存變數
- String tempPath = MyFileUtil.FILE_PATH + File.separator + "temp" + File.separator
- + System.currentTimeMillis() + ".png";
- String parentPath = MyFileUtil.FILE_PATH + File.separator + "temp" + File.separator;
- File parentFile = new File(parentPath);
- if (!(parentFile.exists() && parentFile.isDirectory())) {
- parentFile.mkdir();
- }
- // 按比例縮放(放大縮小)false為縮小,true為放大
- ImageUtils.scale(originalName, tempPath, 4, false);
- ImageHelper.cutCenterImage(tempPath, smallImgPath, ImageUtils.SMALL_IMG_WIDTH,
- ImageUtils.SMALL_IMG_HEIGHT);
- new File(tempPath).deleteOnExit();
- }
- }
- logger.info(imgPath + " 圖片上傳成功");
- return imgPath;
- } else {
- if (mpf.getSize() != imgSzie) {
- logger.error("#######圖片上傳失敗,伺服器獲取到圖片大小(位元組): " + mpf.getSize() + ", 上傳圖片實際大小(位元組): " + imgSzie);
- } else {
- logger.error("單張照片不得超過30M");
- }
- returnnull;
- }
- } catch (IOException e) {
- logger.error(e.getMessage());
- returnnull;
- }
- }
- logger.error("沒有上傳的圖片");
- returnnull;
- }
- /**
- * 獲取縮圖路徑
- * <br>
- * @param originalName
- * @return
- */
- private String getSmallImgName(String originalName) {
- if (StringUtils.isBlank(originalName))
- returnnull;
- int