<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();
}
}