Commit 7cc6d569 by zhoujinghao

上传下载相关代码提交

1 parent 15251a0a
...@@ -30,16 +30,14 @@ public class AttController { ...@@ -30,16 +30,14 @@ public class AttController {
@Autowired @Autowired
private AttService attService; private AttService attService;
/**
* /**上传文件到ftp
* @name: uploadToFtp
* @description: 上传文件到ftp
* @params: []
* @return: java.lang.String
* @date: 2019/12/18 15:48
* @auther:
* *
**/ * @param description 描述
* @param userId 用户id
* @param files 文件
* @return
*/
@ApiOperation( value = "上传文件到ftp",notes = "上传文件到ftp") @ApiOperation( value = "上传文件到ftp",notes = "上传文件到ftp")
@PostMapping(value = "/uploadToFtp",headers = "content-type=multipart/form-data") @PostMapping(value = "/uploadToFtp",headers = "content-type=multipart/form-data")
@ResponseBody @ResponseBody
...@@ -55,13 +53,18 @@ public class AttController { ...@@ -55,13 +53,18 @@ public class AttController {
/** /**
* 下载ftp上文件到本地 * 下载ftp上文件到本地
* @return *@param newFileName 新文件名
*@param fileName 原文件(路径+文件名)
*@param downUrl 下载路径
*@return
*/ */
@ApiOperation( value = "下载ftp上文件到本地",notes = "下载ftp上文件到本地") @ApiOperation( value = "下载ftp上文件到本地",notes = "下载ftp上文件到本地")
@PostMapping(value = "/downloadFile",headers = "content-type=multipart/form-data") @PostMapping(value = "/downloadFile",headers = "content-type=multipart/form-data")
@ResponseBody @ResponseBody
public ApiResponse downloadFile(@RequestParam("files") @Validated MultipartFile[] files) { public ApiResponse downloadFile(@RequestParam("newFileName") String newFileName,
ApiResponse apiResponse = attService.downloadFile(files); @RequestParam("fileName") String fileName,
@RequestParam("downUrl") String downUrl) {
ApiResponse apiResponse = attService.downloadFile(newFileName,fileName,downUrl);
return apiResponse; return apiResponse;
} }
} }
......
...@@ -17,7 +17,7 @@ import java.util.List; ...@@ -17,7 +17,7 @@ import java.util.List;
*/ */
public interface AttService { public interface AttService {
//下载ftp上文件到本地 //下载ftp上文件到本地
ApiResponse downloadFile(MultipartFile[] files); ApiResponse downloadFile(String newFileName, String fileName, String downUrl);
//上传文件到ftp //上传文件到ftp
ApiResponse uploadToFtp(MultipartFile[] files, String description, Long userId); ApiResponse uploadToFtp(MultipartFile[] files, String description, Long userId);
......
...@@ -24,9 +24,7 @@ import org.springframework.stereotype.Service; ...@@ -24,9 +24,7 @@ import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.io.File; import java.io.*;
import java.io.IOException;
import java.io.InputStream;
import java.util.*; import java.util.*;
/** /**
...@@ -53,22 +51,35 @@ public class AttServiceImpl implements AttService { ...@@ -53,22 +51,35 @@ public class AttServiceImpl implements AttService {
private AttachmentInfoMapper infoMapper; private AttachmentInfoMapper infoMapper;
/** /**
* 下载ftp上文件到本地 * 下载ftp上文件到本地
* @return *@param newFileName 新文件名
*@param fileName 原文件(路径+文件名)
*@param downUrl 下载路径
*@return
*/ */
@Override @Override
public ApiResponse downloadFile(MultipartFile[] files) { public ApiResponse downloadFile(String newFileName, String fileName, String downUrl) {
// for (MultipartFile file:files) { log.info("下载文件参数newFileName:{}fileName:{}downUrl:",newFileName,fileName,downUrl);
// FtpOperationUtils ftpOperationUtils = new FtpOperationUtils(); OutputStream os=null;
// try { String lastPath="";
// String originalFilename = file.getOriginalFilename(); File localFile = new File(downUrl + "/" + newFileName);
// InputStream inputStream = ftpOperationUtils.downloadFile(originalFilename); if (!localFile.getParentFile().exists()){//文件夹目录不存在创建目录
// FileUtil.writeFromStream(inputStream, "D:\\a1.txt"); localFile.getParentFile().mkdirs();
// } catch (Exception e) { //存放图片的路径地址
// e.printStackTrace(); StringBuilder sb = new StringBuilder(ftpPath).append("/");
// } String path = sb.toString();
// } lastPath=path;
try {
return null; localFile.createNewFile();
os = new FileOutputStream(localFile);
FtpUtil fileFtp = new FtpUtil();
log.info("通过ftp下载文件参数lastPath:{}ileName:{}downUrl:",lastPath,fileName,downUrl);
fileFtp.downloadFile(lastPath,fileName,downUrl);
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return new ApiResponse(ApiResponse.SUCCESS,ApiResponse.SUCCESS_TEXT);
} }
/** /**
...@@ -99,7 +110,7 @@ public class AttServiceImpl implements AttService { ...@@ -99,7 +110,7 @@ public class AttServiceImpl implements AttService {
if (!extList.contains(suffixes)) { if (!extList.contains(suffixes)) {
return new ApiResponse(ApiResponse.FAIL, MessageEnum.FILE_PICTURE.getRemarks()); return new ApiResponse(ApiResponse.FAIL, MessageEnum.FILE_PICTURE.getRemarks());
} }
//图片名称重新命名包括后缀名 //重新命名包括后缀名
String newName = StringUtil.getRandomOrderNumber() + "_" + StringUtil.getRandomOrderNumberString() + suffixes; String newName = StringUtil.getRandomOrderNumber() + "_" + StringUtil.getRandomOrderNumberString() + suffixes;
// String newName= UUID.randomUUID().toString().replaceAll("\\-", "")+suffixes; // String newName= UUID.randomUUID().toString().replaceAll("\\-", "")+suffixes;
newNameList.add(newName); newNameList.add(newName);
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!