java从FTP下载文件功能
来源:互联网 发布:平面设计网络课程 编辑:程序博客网 时间:2024/05/18 12:35
java下载文件功能,导出的时候会下载到浏览器默认下载文件夹里,360浏览器会弹出一个保存文件的弹出框
//下载导出报表
//JS
function downloadExport(filename){
window.location.href=”/order-center/ui/spm/downloadExport.do?filename=”+filename;
}
/** * 查询导出报表: http://localhost:8080/order-center/ui/spm/downloadExport.do * key module */@RequestMapping(value = "downloadExport.do", method = RequestMethod.GET)public void downloadExport( @RequestParam(value = "filename", required = false) String filename, HttpServletResponse response) { File pf = new File(SpmRest.class.getResource("/").getFile()); FileInputStream fis=null; OutputStream ostream=null; try {// if (!getUser().getShopid().equals(EGlobal.TOP_SHOP_ID)) {//// shopId = getUser().getShopid();// shopId = spmService.getUserShopList(getUser(),shopId);// } String dirPath = pf.getParentFile().getParentFile().getAbsolutePath()+ File.separator + "exportfile"; File dirFile = new File(dirPath); if (!dirFile.exists()) dirFile.mkdir(); File file = new File(dirFile.getAbsolutePath() + File.separator + filename); if (!file.exists()) {//查询服务器有没有文件,没有则冲FTP下载 boolean downloadExport = spmService.downloadExport(dirPath, filename); if(!downloadExport){ return ; } } fis = new FileInputStream(file); ostream = response.getOutputStream(); response.setContentType("application/ms-excel"); response.setHeader("Content-disposition", "attachment; filename="+ filename); byte[] b = new byte[1024]; int len = fis.read(b); while (len > 0) { ostream.write(b, 0, len); len = fis.read(b); } } catch (IOException e) { e.printStackTrace(); } finally { try { fis.close(); ostream.close(); } catch (IOException e) { e.printStackTrace(); } }}/** * 从ftp下载报表 * @throws SftpException */public boolean downloadExport(String dirPath, String filename) { boolean flag = false; ChannelSftp sftp = null; SFTPUtilTools sftptool = null;//连接FTP,自定义类,网上很多示例 try{ sftptool = new SFTPUtilTools(); sftp = sftptool.connectChannel(FGlobal.EXPORT_HOSTNAME, FGlobal.EXPORT_USERNAME, FGlobal.EXPORT_PASSWORD); if (sftp == null) { logger.warn("无效链接或链接失败"); return flag; } logger.info("**************开始下载导出报表EXPORT**************"); sftp.cd(FGlobal.EXPORT_REC);//进入ftp下载文件夹 File file = new File(dirPath+File.separator+filename);//导出到服务器文件 sftp.get(filename,new FileOutputStream(file));//下载文件 System.out.println("文件更新成功:" + file.getName()); flag = true; logger.info("**************下载文件成功**************"); }catch(Exception e){ logger.error(e); }finally{ sftptool.closeChannel(sftp); } return flag;}
阅读全文
0 0
- java从FTP下载文件功能
- 从ftp下载文件
- FTP 文件的下载功能
- 从FTP服务器下载文件
- 从ftp服务器下载文件
- JAVA实现FTP服务器文件的上传,下载,删除功能
- JAVA FTP 下载文件
- java ftp 文件下载
- java 远程从FTP服务器下载文件到本地
- C#给ftp上传、下载文件功能
- java实现ftp下载文件
- java读取ftp文件下载
- java中FTP下载文件
- java关于ftp文件下载
- java中FTP下载文件
- 从FTP下载文件(C++,Win32)
- 批处理从FTP下载 最新文件
- 用多线程分块从ftp下载文件!
- 学生管理系统
- Spring Cloud入门三- Zuul作api-gateway
- python-安装pip以及setuptools
- 鼠标拖拽窗口效果(不允许拖出屏幕的可视区域)
- 前台控制开始日期小于结束日期
- java从FTP下载文件功能
- 5个月ReactNative感受
- linux 下配置opencv3.3.0及 opencv_contrib
- 子元素浮动父容器高度不能自适应的CSS解决方法
- Android 达到Toolbar效果的demo
- 图像处理之gamma校正
- 跳转应用商店 对应页面
- Linux环境下的逻辑卷管理LVM的几个术语
- 关于web service