struts2下载文件问题

zjc198805 zjc198805 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
action中代码:
public String downLoadComplex()
{
String [][]data=null;
String urlPath=null;
String minTime=(String)request.getSession().getAttribute("lastMonth");
String maxTime=(String)request.getSession().getAttribute("yesterday");
data=statisticService.getCsv(num,id, FileName.map.get(Integer.parseInt(num)),type_v,type_p,type_r,type_r1,minTime,maxTime);
response.setCharacterEncoding("UTF-8");
String date=new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date());
if(data!=null)
urlPath=getCsv(data,date);
try {
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("application/vnd.ms-excel");
//response.setContentType("application/octet-stream;charset=UTF-8");
// response.setContentType("application/vnd.rn-realmedia-vbr");
response.setHeader("Content-disposition","inline;filename=\"" + data[0][0] + " ( " + date + ").csv" + "\"");

OutputStream outputStream = response.getOutputStream();
InputStream inputStream = new FileInputStream(request.getRealPath("/")+urlPath);
byte[] buffer = new byte[1024];
int i = -1;
while ((i = inputStream.read(buffer)) != -1)
{
outputStream.write(buffer, 0, i);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;

}
@SuppressWarnings("deprecation")
private String getCsv(String[][] data,String date)
{
String basePath = request.getRealPath("/");
String csvPath = basePath + "csv_data";

File tempFile = new File(csvPath);
tempFile.mkdirs();
File finalFile = new File(csvPath +"/" + data[0][0] + "("+date+").csv");
try {
CSVWriter writer = new CSVWriter(new FileWriter(finalFile));
for (int i = 0; i < data.length; i++)
{
writer.writeNext(data[i]);
}
writer.close();

} catch (IOException e) {
e.printStackTrace();
}
String urlPath="csv_data/" + data[0][0] + "("+date+").csv";
return urlPath;

}

迅雷文件名称多了一个后缀名(.action)
...全文
评论
收藏 已收藏
分享
返回顶部
zjc198805
Java EE 67,535 成员 225,843 内容