Commit 543a9731 by 284718418@qq.com

优化代码-修改下载BUG

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