Browse Source

ftp file down -sxm 2021-11-1

master
[li_she] 4 years ago
parent
commit
ded34f43a6
  1. 23
      src/main/java/com/gaotao/modules/ftp/controller/SysFtpController.java
  2. 55
      src/main/java/com/gaotao/modules/ftp/util/FTPUtils.java

23
src/main/java/com/gaotao/modules/ftp/controller/SysFtpController.java

@ -13,6 +13,7 @@ import com.gaotao.modules.sys.controller.AbstractController;
import com.gaotao.modules.sys.entity.SysUserEntity;
import com.gaotao.modules.sys.service.SysConfigService;
import lombok.extern.slf4j.Slf4j;
import oracle.jdbc.proxy.annotation.Post;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@ -144,20 +145,18 @@ public class SysFtpController extends AbstractController {
}
/**
* @Method upload
* @Description: FTP文件下载
* @author zuowenwen
* @Method downFtpFile
* @Description: ftp 文件流下载 (返回给前端一个文件流)
* @author sxm
* @Version 1.0
* @param id
* @param response
* @return com.gaotao.common.utils.R
* @param ossEntity
* @return java.io.File
* @throws
* @date 2021/9/29
* @date 2020/12/17
*/
@GetMapping("/downFtpFile")
public R upload(@RequestParam("ossEntity") Long id ,HttpServletResponse response){
SysOssEntity resultData =sysOssService.getById(id);
R r = FTPUtils.downFile("",resultData.getNewFileName(),resultData.getFileName());
return r;
@PostMapping("/downFtpFile")
public void downFtpFile(@RequestBody SysOssEntity ossEntity, HttpServletResponse response){
SysOssEntity resultData =sysOssService.getById(ossEntity.getId());
FTPUtils.downFile(response,"",resultData.getNewFileName());
}
}

55
src/main/java/com/gaotao/modules/ftp/util/FTPUtils.java

@ -439,11 +439,7 @@ public class FTPUtils {
log.info("文件开始下载!文件名称:"+fileName);
isExist = true;
InputStream fis = ftpClient.retrieveFileStream(ftpFiles[i].getName());
// InputStream fis = new BufferedInputStream(new FileInputStream(temp_file));
byte[] buffer = new byte[fis.available()];
// fis.read(buffer);
// fis.close();
// 取得文件名
// 清空response
response.reset();
if(newFileName != null && !"".equals(newFileName)){
@ -480,7 +476,6 @@ public class FTPUtils {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
//关闭流和链接
try {
// if(fos!=null){
@ -489,11 +484,9 @@ public class FTPUtils {
// }
if(ftpClient!=null){
ftpClient.logout();
// ftpClient.disconnect();
}
} catch (IOException e) {
log.info("关闭流和链接出错!错误信息:"+e.getMessage());
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@ -502,15 +495,17 @@ public class FTPUtils {
}
public static R downFile( String path, String fileName, String newFileName){
R r = new R();
/**
* @Desc ftp文件下载
* @param response
* @param path
* @param fileName
*/
public static void downFile( HttpServletResponse response, String path, String fileName){
FTPClient ftpClient = getFTPClient();
//判断链接是否关闭
if(!ftpClient.isConnected()){
log.info("ftp链接出错!链接已关闭!");
r.put("msg" ,"ftp链接出错!");
r.put("success", false);
return r;
}
//进入工作空间
boolean flag = true;
@ -521,12 +516,10 @@ public class FTPUtils {
}
String name=new String(path.getBytes("UTF-8"),"iso-8859-1");
flag = ftpClient.changeWorkingDirectory(name);
// flag = ftpClient.changeWorkingDirectory("/");
if(!flag){
log.info("ftp链接出错!文件夹不存在!");
r.put("msg" ,"文件件不存在!");
r.put("success", false);
return r;
}
//获取当前目录下的所有文件名称
ftpClient.sendCommand("OPTS UTF8 ON");
@ -540,16 +533,32 @@ public class FTPUtils {
log.info("文件开始下载!文件名称:"+fileName);
isExist = true;
InputStream fis = ftpClient.retrieveFileStream(ftpFiles[i].getName());
r.put("file",fis);
byte[] buffer = new byte[fis.available()];
// 清空response
response.reset();
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
response.addHeader("Content-Length", "" + ftpFiles[i].getSize());
response.setContentType("application/octet-stream");
response.setCharacterEncoding("UTF-8");
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
int length;
while((length=fis.read(buffer))>0){ //把文件流写到缓冲区里
toClient.write(buffer,0,length);
}
toClient.write(buffer);
toClient.flush();
fis.close();
toClient.close();
ftpClient.completePendingCommand();;
log.info("下载完成!文件名称:"+fileName);
}
}
ftpClient.completePendingCommand();
if(isExist){
r.put("msg" ,"ftp下载成功!");
r.put("success", true);
log.info("下载完成!文件名称:"+fileName);
}else{
r.put("msg" ,"ftp下载失败!");
r.put("success", false);
log.info("下载失败!文件名称:"+fileName);
}
} catch (IOException e) {
log.info("ftp下载出错!错误信息:"+e.getMessage());
@ -559,7 +568,7 @@ public class FTPUtils {
//关闭流和链接
try {
if(ftpClient!=null){
ftpClient.logout();
ftpClient.logout();
}
} catch (IOException e) {
log.info("关闭流和链接出错!错误信息:"+e.getMessage());
@ -567,8 +576,6 @@ public class FTPUtils {
e.printStackTrace();
}
}
return r;
}
}
Loading…
Cancel
Save