|
|
@@ -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());
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
}
|