Browse Source

将上传文件的方式切换成使用https的方式传输不同过dubbo方式

yujia 8 years ago
parent
commit
bfa075cd27

+ 1 - 1
src/main/java/com/uas/platform/b2c/common/base/api/FileUploadController.java

@@ -49,7 +49,7 @@ public class FileUploadController {
 		for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
 			MultipartFile mf = entity.getValue();
 			try {
-				String path = fileService.save(mf.getOriginalFilename(), mf.getBytes());
+				String path = fileService.save(mf);
 				Map<String, Object> map = new HashMap<String, Object>();
 				map.put("path", path);
 				map.put("fileName", mf.getOriginalFilename());

+ 10 - 2
src/main/java/com/uas/platform/b2c/common/base/service/FileService.java

@@ -1,6 +1,7 @@
 package com.uas.platform.b2c.common.base.service;
 
 import com.uas.platform.b2c.common.base.model.FileUpload;
+import org.springframework.web.multipart.MultipartFile;
 
 /**
  * 文件上传
@@ -18,10 +19,17 @@ public interface FileService {
 
     /**
      * 保存文件
-     * @param fileName 文件名
-     * @param fileBytes 文件字节数组
+     * @param mf 需要上传的文件
      * @return 文件路径
      */
+    public String save(MultipartFile mf);
+
+    /**
+     * 用户上传文件
+     * @param fileName 文件名
+     * @param fileBytes 字节数组
+     * @return String
+     */
     public String save(String fileName, byte[] fileBytes);
 
 }

+ 6 - 0
src/main/java/com/uas/platform/b2c/common/base/service/ImageService.java

@@ -39,5 +39,11 @@ public interface ImageService {
 	 */
 	public IPicture save(String fileName, byte[] fileBytes);
 
+	/**
+	 * 保存图片
+	 * @param mf MultipartFile 文件的信息
+	 * @return
+	 * @throws Exception
+	 */
     IPicture uploadFile(MultipartFile mf) throws Exception;
 }

+ 51 - 53
src/main/java/com/uas/platform/b2c/common/base/service/impl/DFSImageServiceImpl.java

@@ -13,7 +13,6 @@ import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.FilenameUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
-import org.springframework.web.client.RestTemplate;
 import org.springframework.web.multipart.MultipartFile;
 
 import java.io.File;
@@ -24,63 +23,62 @@ import java.util.List;
 @Service
 public class DFSImageServiceImpl implements ImageService {
 
-	@Autowired
-	private FileClient fileClient;
+    @Autowired
+    private FileClient fileClient;
 
-	@Autowired
-	private RestTemplate restTemplate;
+    @Autowired
+    private DBPictureRepository pictureRepository;
 
-	@Autowired
-	private DBPictureRepository pictureRepository;
+    @Override
+    public List<DBPicture> save(List<File> files) {
+        List<DBPicture> pictures = new ArrayList<DBPicture>();
+        for (File file : files) {
+            try {
+                HttpUtils.Response response = HttpUtils.upload(FileUrl.FILE_UPLOAD, file, null);
+                if (response.getStatusCode() == 200) {
+                    JSONObject obj = FastjsonUtils.parseObject(response.getResponseText());
+                    String fileUrl = (String) obj.get("path");
+                    pictures.add(new DBPicture(file.getName(), fileUrl, com.uas.platform.core.util.FileUtils.getImagePixel(file)));
+                } else {
+                    throw new IllegalStateException(response.getResponseText());
+                }
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        }
+        return pictureRepository.save(pictures);
+    }
 
-	@Override
-	public List<DBPicture> save(List<File> files) {
-		List<DBPicture> pictures = new ArrayList<DBPicture>();
-		for (File file : files) {
-			try {
-				String fileUrl = fileClient.uploadImage(FileUtils.readFileToByteArray(file), file.length(),
-						FilenameUtils.getExtension(file.getName()), null);
-				pictures.add(new DBPicture(file.getName(), fileUrl, com.uas.platform.core.util.FileUtils.getImagePixel(file)));
-			} catch (IOException e) {
-				e.printStackTrace();
-			}
-		}
-		return pictureRepository.save(pictures);
-	}
+    @Override
+    public DBPicture save(File file) {
+        try {
+            String fileUrl = fileClient.uploadImage(FileUtils.readFileToByteArray(file), file.length(),
+                    FilenameUtils.getExtension(file.getName()), null);
+            return pictureRepository.save(new DBPicture(file.getName(), fileUrl, com.uas.platform.core.util.FileUtils.getImagePixel(file)));
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        return null;
+    }
 
-	@Override
-	public DBPicture save(File file) {
-		try {
-			String fileUrl = fileClient.uploadImage(FileUtils.readFileToByteArray(file), file.length(),
-					FilenameUtils.getExtension(file.getName()), null);
-			return pictureRepository.save(new DBPicture(file.getName(), fileUrl, com.uas.platform.core.util.FileUtils.getImagePixel(file)));
-		} catch (IOException e) {
-			e.printStackTrace();
-		}
-		return null;
-	}
+    @Override
+    public IPicture save(String fileName, byte[] fileBytes) {
+        String fileUrl = fileClient.uploadImage(fileBytes, fileBytes.length, FilenameUtils.getExtension(fileName), null);
+        return pictureRepository.save(new DBPicture(fileName, fileUrl, com.uas.platform.core.util.FileUtils.getImagePixel(fileBytes)));
+    }
 
-	@Override
-	public IPicture save(String fileName, byte[] fileBytes) {
-		String fileUrl = fileClient.uploadImage(fileBytes, fileBytes.length, FilenameUtils.getExtension(fileName), null);
-		restTemplate.postForObject(FileUrl.FILE_UPLOAD, null, Object.class, fileBytes);
+    @Override
+    public IPicture uploadFile(MultipartFile mf) throws Exception {
+        HttpUtils.Response response = HttpUtils.upload(FileUrl.FILE_UPLOAD, mf, null);
+        if (response.getStatusCode() == 200) {
+            JSONObject obj = FastjsonUtils.parseObject(response.getResponseText());
 
-		restTemplate.postForEntity(FileUrl.FILE_UPLOAD, null, Object.class, fileBytes);
-		return pictureRepository.save(new DBPicture(fileName, fileUrl, com.uas.platform.core.util.FileUtils.getImagePixel(fileBytes)));
-	}
-
-	@Override
-	public IPicture uploadFile(MultipartFile mf) throws Exception {
-		HttpUtils.Response response = HttpUtils.upload(FileUrl.FILE_UPLOAD, mf, null);
-		if (response.getStatusCode() == 200) {
-			JSONObject obj = FastjsonUtils.parseObject(response.getResponseText());
-
-			String fileName = mf.getOriginalFilename();
-			String fileUrl = (String) obj.get("path");
-			return pictureRepository.save(new DBPicture(fileName, fileUrl, com.uas.platform.core.util.FileUtils.getImagePixel(mf.getBytes())));
-		} else {
-			throw new IllegalStateException(response.getResponseText());
-		}
-	}
+            String fileName = mf.getOriginalFilename();
+            String fileUrl = (String) obj.get("path");
+            return pictureRepository.save(new DBPicture(fileName, fileUrl, com.uas.platform.core.util.FileUtils.getImagePixel(mf.getBytes())));
+        } else {
+            throw new IllegalStateException(response.getResponseText());
+        }
+    }
 
 }

+ 31 - 16
src/main/java/com/uas/platform/b2c/common/base/service/impl/FileServiceImpl.java

@@ -1,15 +1,17 @@
 package com.uas.platform.b2c.common.base.service.impl;
 
+import com.alibaba.fastjson.JSONObject;
 import com.uas.dfs.service.FileClient;
 import com.uas.platform.b2c.common.base.constant.FileUrl;
 import com.uas.platform.b2c.common.base.model.FileUpload;
 import com.uas.platform.b2c.common.base.service.FileService;
+import com.uas.platform.b2c.core.utils.FastjsonUtils;
+import com.uas.platform.b2c.core.utils.HttpUtils;
 import com.uas.platform.core.exception.IllegalOperatorException;
 import org.apache.commons.io.FilenameUtils;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Service;
-import org.springframework.web.client.RestTemplate;
+import org.springframework.web.multipart.MultipartFile;
 import org.springframework.web.multipart.commons.CommonsMultipartFile;
 
 @Service
@@ -18,16 +20,37 @@ public class FileServiceImpl implements FileService {
 	@Autowired
 	private FileClient fileClient;
 
-	@Autowired
-	private RestTemplate restTemplate;
-
 	@Override
 	public String save(FileUpload fileUpload) {
 		CommonsMultipartFile file = fileUpload.getFile();
 		try {
 			// 上传到文件系统
-			return fileClient.upload(file.getBytes(), file.getSize(),
-					FilenameUtils.getExtension(file.getOriginalFilename()), null);
+			HttpUtils.Response response = HttpUtils.upload(FileUrl.FILE_UPLOAD, file, null);
+			if (response.getStatusCode() == 200) {
+				JSONObject obj = FastjsonUtils.parseObject(response.getResponseText());
+				String fileUrl = (String) obj.get("path");
+				return fileUrl;
+			} else {
+				throw new IllegalStateException(response.getResponseText());
+			}
+		} catch (Exception e) {
+			e.printStackTrace();
+			throw new IllegalOperatorException("附件上传失败");
+		}
+	}
+
+	@Override
+	public String save(MultipartFile mf) {
+		try {
+            HttpUtils.Response response = HttpUtils.upload(FileUrl.FILE_UPLOAD, mf, null);
+            if (response.getStatusCode() == 200) {
+                JSONObject obj = FastjsonUtils.parseObject(response.getResponseText());
+
+                String fileUrl = (String) obj.get("path");
+                return fileUrl;
+            }else {
+                throw new IllegalStateException(response.getResponseText());
+            }
 		} catch (Exception e) {
 			e.printStackTrace();
 			throw new IllegalOperatorException("附件上传失败");
@@ -37,15 +60,7 @@ public class FileServiceImpl implements FileService {
 	@Override
 	public String save(String fileName, byte[] fileBytes) {
 		try {
-//			FilenameUtils.getExtension(fileName);
-//			FileBody fileBody = new FileBody(new File(filePath));
-//			MultipartEntityBuilder builder = MultipartEntityBuilder.create();
-//			builder.addPart("file", fileBody);
-//			HttpEntity reqEntity = builder.build();
-			ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(FileUrl.FILE_UPLOAD, null, String.class, fileBytes);
-			System.out.println(stringResponseEntity.getBody());
-			// String path = fileClient.upload(fileBytes, fileBytes.length, FilenameUtils.getExtension(fileName), null);
-			return null;
+            return fileClient.upload(fileBytes, fileBytes.length, FilenameUtils.getExtension(fileName), null);
 		} catch (Exception e) {
 			e.printStackTrace();
 			throw new IllegalOperatorException("附件上传失败");

+ 53 - 0
src/main/java/com/uas/platform/b2c/core/utils/HttpUtils.java

@@ -193,6 +193,59 @@ public class HttpUtils {
 		}
 	}
 
+
+	/**
+	 * http上传文件
+	 *
+	 * @param url
+	 *            请求地址
+	 * @param file
+	 *            文件
+	 * @param params
+	 *            参数
+	 * @return
+	 * @throws Exception
+	 * @throws IOException
+	 * @throws IllegalStateException
+	 */
+	public static Response upload(String url, File file, Map<String, String> params) throws IllegalStateException, IOException,
+			Exception {
+		CloseableHttpClient httpClient = null;
+		CloseableHttpResponse response = null;
+		try {
+			httpClient = HttpClients.createDefault();
+			HttpPost httpPost = new HttpPost(url);
+			FileBody fileBody = new FileBody(file);
+			MultipartEntityBuilder builder = MultipartEntityBuilder.create();
+			builder.addPart("file", fileBody);
+			if (params != null) {
+				for (String paramKey : params.keySet()) {
+					StringBody body = new StringBody(params.get(paramKey), ContentType.create("text/plain", Consts.UTF_8));
+					builder.addPart(paramKey, body);
+				}
+			}
+			HttpEntity reqEntity = builder.build();
+			httpPost.setEntity(reqEntity);
+			response = httpClient.execute(httpPost);
+			return Response.getResponse(response);
+		} finally {
+			try {
+				if (response != null) {
+					response.close();
+				}
+			} catch (IOException e) {
+				e.printStackTrace();
+			}
+			try {
+				if (httpClient != null) {
+					httpClient.close();
+				}
+			} catch (IOException e) {
+				e.printStackTrace();
+			}
+		}
+	}
+
 	/**
 	 * http上传文件
 	 *