Commit c0d70c7a by 284718418@qq.com

优化代码-修改下载BUG

parent 106f396d
...@@ -126,7 +126,7 @@ public class DiskFilesController { ...@@ -126,7 +126,7 @@ public class DiskFilesController {
} }
return ResultUtil.error("上传文件失败"); return ResultUtil.error("上传文件失败");
} }
/* @ResponseBody /*@ResponseBody
@GetMapping("/download") @GetMapping("/download")
@ApiOperation(value = "下载普通文件", httpMethod = "POST", notes = "接口发布说明") @ApiOperation(value = "下载普通文件", httpMethod = "POST", notes = "接口发布说明")
public Result<Object> download(@CurrentUser UserBean userBean, public Result<Object> download(@CurrentUser UserBean userBean,
...@@ -140,7 +140,7 @@ public class DiskFilesController { ...@@ -140,7 +140,7 @@ public class DiskFilesController {
return ResultUtil.error("下载失败,文件不存在"); return ResultUtil.error("下载失败,文件不存在");
} }
try { try {
boolean count = ftpService.downloadFile(diskFiles.getDiskPath(),localPath,diskFiles.getTitle()+diskFiles.getFileType()); boolean count = ftpService.downloadFile(diskFiles.getDiskPath());
if(count){ if(count){
DiskFilesLog diskFilesLog = new DiskFilesLog(); DiskFilesLog diskFilesLog = new DiskFilesLog();
//新增 文件浏览记录 //新增 文件浏览记录
...@@ -232,7 +232,7 @@ public class DiskFilesController { ...@@ -232,7 +232,7 @@ public class DiskFilesController {
if(StringUtils.isEmpty(diskFiles)){ if(StringUtils.isEmpty(diskFiles)){
throw new CustomException("下载失败,文件不存在"); throw new CustomException("下载失败,文件不存在");
} }
FileInputStream fis = null; InputStream fis = null;
BufferedInputStream bis = null; BufferedInputStream bis = null;
OutputStream os; OutputStream os;
File filePath = null; File filePath = null;
...@@ -241,9 +241,10 @@ public class DiskFilesController { ...@@ -241,9 +241,10 @@ public class DiskFilesController {
resp.setContentType("application/octet-stream"); resp.setContentType("application/octet-stream");
resp.setHeader("content-disposition", "attachment; filename=" + new String(name.getBytes("UTF8"), "ISO-8859-1")); resp.setHeader("content-disposition", "attachment; filename=" + new String(name.getBytes("UTF8"), "ISO-8859-1"));
resp.setCharacterEncoding("UTF-8"); resp.setCharacterEncoding("UTF-8");
filePath = new File(diskFiles.getDiskPath()); // filePath = new File(diskFiles.getDiskPath());
byte[] buffer = new byte[1024]; byte[] buffer = new byte[1024];
fis = new FileInputStream(filePath); // fis = new FileInputStream(filePath);
fis = ftpService.downloadFile(diskFiles.getDiskPath());
bis = new BufferedInputStream(fis); bis = new BufferedInputStream(fis);
os = resp.getOutputStream(); os = resp.getOutputStream();
int i = bis.read(buffer); int i = bis.read(buffer);
......
...@@ -5,6 +5,7 @@ import com.jcraft.jsch.ChannelSftp; ...@@ -5,6 +5,7 @@ import com.jcraft.jsch.ChannelSftp;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.File; import java.io.File;
import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.List; import java.util.List;
...@@ -27,11 +28,9 @@ public interface FtpService { ...@@ -27,11 +28,9 @@ public interface FtpService {
* 下载单个文件 * 下载单个文件
* *
* @param remotePath:远程目录 * @param remotePath:远程目录
* @param localPath:本地保存目录 * @return InputStream
* @param localFileName:保存文件名
* @return
*/ */
boolean downloadFile(String remotePath, String localPath, String localFileName); InputStream downloadFile(String remotePath);
/** /**
* 删除服务器文件 * 删除服务器文件
......
...@@ -113,39 +113,29 @@ public class FtpServiceImpl implements FtpService { ...@@ -113,39 +113,29 @@ public class FtpServiceImpl implements FtpService {
* 下载单个文件 * 下载单个文件
* *
* @param remotePath:远程下载目录 * @param remotePath:远程下载目录
* @param localPath:本地保存目录(以路径符号结束)
* @param localFileName:保存文件名
* @return * @return
*/ */
@Override @Override
public boolean downloadFile(String remotePath, String localPath, String localFileName) { public InputStream downloadFile(String remotePath) {
FileOutputStream fieloutput = null;
ChannelSftp sftp = config.getSftpSocket(); ChannelSftp sftp = config.getSftpSocket();
InputStream nputStream = null;
try { try {
// sftp.cd(remotePath); // sftp.cd(remotePath);
File file = new File(localPath + localFileName);
// mkdirs(localPath + localFileName); // mkdirs(localPath + localFileName);
fieloutput = new FileOutputStream(file); //sftp.get(remotePath, fieloutput);
sftp.get(remotePath, fieloutput); nputStream = sftp.get(remotePath);
if (log.isInfoEnabled()) { if (log.isInfoEnabled()) {
log.info("===DownloadFile:" + remotePath + " success from sftp."); log.info("===DownloadFile:" + remotePath + " success from sftp.");
} }
return true; return nputStream;
} catch (FileNotFoundException e) { } catch ( SftpException e) {
e.printStackTrace(); e.printStackTrace();
} catch (SftpException e) { } catch (Exception e){
e.printStackTrace(); e.printStackTrace();
} finally { }finally {
if (null != fieloutput) {
try {
fieloutput.close();
} catch (IOException e) {
e.printStackTrace();
}
}
config.returnSftpSocket(sftp); config.returnSftpSocket(sftp);
} }
return false; return nputStream;
} }
@Override @Override
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment