从服务器上下载文件到本地,项目中用得比较多,记录一下

<td><a href="#" class="easyui-linkbutton btn-redo" iconCls="icon-ed" style="width: 70%; height: 90%;float: right;font-size: 300%;"
                           <c:if test="${'1'!=GSPO074Fintoerpflg}">disabled="disabled"</c:if>
                           onclick="todownload()">下载发送清单</a></td>

//下载发送清单
function todownload() {

	var form = $("<form>");
	form.attr("style","display:none");
	form.attr("target","");
	form.attr("method","post");
	form.attr("action",rootPath+'a/ordermanage/GSPO074/todownload');
	var input1 = $("<input>");
	input1.attr("type","hidden");
	input1.attr("name","receiveddate");
	input1.attr("value",time);
	$("body").append(form);
	form.append(input1);
	form.submit();
}

 

 

@RequestMapping("todownload")
	@ResponseBody
	public String todownload(GSPO071Entity entity, HttpServletRequest request, HttpServletResponse response)  {
		MessageInfo msgInfo = new MessageInfo();//消息类
		msgInfo.setSuccess(false);

		try {
			//获取时间
			String receiveddate = DateUtils.getDate("yyyyMM");

			Properties props = new Properties();
			props.load(GSPO074Service.class.getClassLoader().getResourceAsStream("destinationPath.properties"));

			String SaveFilePath = props.getProperty("CSV_Backup_Path");

			File file_dic = new File(SaveFilePath);
			String[] filelist = file_dic.list();
			boolean file_exist = false;
			if(filelist != null) {
				for(int i = 0; i < filelist.length; i++) {
					String file_exist_name = filelist[i];
					int index = file_exist_name.indexOf(".");
					String before1 = file_exist_name.substring(0, 6);

					if(receiveddate.equals(before1)) {
						downloadFile(SaveFilePath+file_exist_name,response);
						file_exist = true;
						msgInfo.setSuccess(true);
					}
				}
			}
			if(file_exist) {
				msgInfo.setMessageContent("下载完成");
			}else {
				msgInfo.setMessageContent("下载失败,未找到可下载的清单!");
			}

		}  catch (BusinessException ex) {
			msgInfo = ex.getMsgInfo(); // 获取消息异常
			this.logUtilService.writeLog("GSPO074-下载", msgInfo);
		} catch (Exception ex){
			ex.printStackTrace();
			msgInfo.setMessageContent(ex.getMessage());
			msgInfo.setSuccess(false);
			this.logUtilService.writeLog("GSPO074-下载", msgInfo);
		}
		return JSONObject.fromObject(msgInfo).toString();

	}

	public void downloadFile(String path,HttpServletResponse response) {
		try {
//			String path = (String) session.getAttribute("path");
			// path是指欲下载的文件的路径。
			File file = new File(path);
			// 取得文件名。
			String filename = file.getName();
			// 取得文件的后缀名。
			String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();

			// 以流的形式下载文件。
			InputStream fis = new BufferedInputStream(new FileInputStream(path));
			byte[] buffer = new byte[fis.available()];
			fis.read(buffer);
			fis.close();
			// 清空response
			response.reset();
			// 设置response的Header
			response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
			response.addHeader("Content-Length", "" + file.length());
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			response.setContentType("application/octet-stream");
			toClient.write(buffer);
			toClient.flush();
			toClient.close();
			System.out.println("压缩包下载成功");
		} catch (IOException ex) {
			ex.printStackTrace();
		}

	}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值