java文件下载改名
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java文件下载改名相关的知识,希望对你有一定的参考价值。
response.setHeader(arg0, arg1) 方法获取文件名,但是我想让下载下来的文件有一个统一的名字,比如 《点击下载》 下载下来一个 XXXXX.txt 文件 代码请详细些 谢谢~
参考技术A 要重新设置contentType,如application/octet-stream,再加一个filenamefilename="XXXXX.txt";
response.setHeader("Content-Disposition", "attachment; filename="+filename); 参考技术B 用同一种方法保存文件名就行了
String name = "UUID.randomUUID() + "." + "txt"";
response.setHeader( "Content-disposition ", "attachment; filename="+name "); 参考技术C response.setHeader(
"Content-Disposition",
"inline; filename=\""
+ URLEncoder.encode(title, "UTF-8")
+ ".txt\""); 参考技术D //
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.plocc.framework.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class FileUtils extends org.apache.commons.io.FileUtils
public FileUtils()
public static String exportExcel(Map<String, Object> dataMap) throws Exception
HSSFWorkbook workbook = null;
File fileOut;
if (null != dataMap.get("tempPath"))
try
fileOut = new File((String)dataMap.get("tempPath"));
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(fileOut));
workbook = new HSSFWorkbook(fs);
catch (Exception var7)
var7.printStackTrace();
throw new Exception("exportExcel---Excel导出出错:" + var7.getMessage());
else
workbook = new HSSFWorkbook();
try
createSheet(workbook, dataMap);
catch (Exception var6)
var6.printStackTrace();
throw new Exception("exportExcel---Excel导出出错:" + var6.getMessage());
fileOut = null;
String filePath = (String)dataMap.get("filePath");
if (StringUtils.isEmpty(filePath))
throw new Exception("exportExcel---Excel导出出错:无效文件路径!");
else
try
FileOutputStream fileOut = new FileOutputStream(filePath);
workbook.write(fileOut);
fileOut.flush();
fileOut.close();
return filePath;
catch (Exception var5)
fileOut.close();
var5.printStackTrace();
throw new Exception("exportExcel---Excel导出出错:" + var5.getMessage());
private static void createSheet(HSSFWorkbook workbook, Map<String, Object> dataMap) throws Exception
HSSFSheet sheet = null;
if (null != dataMap.get("tempPath"))
sheet = workbook.getSheetAt(0);
else
String sheetName = "sheet1";
if (null != dataMap.get("sheetName"))
sheetName = (String)dataMap.get("sheetName");
sheet = workbook.createSheet(sheetName);
HSSFFont font = workbook.createFont();
font.setFontHeightInPoints((short)12);
HSSFCellStyle styleHeader = workbook.createCellStyle();
styleHeader.setAlignment((short)2);
styleHeader.setFont(font);
styleHeader.setFillForegroundColor((short)22);
styleHeader.setFillPattern((short)1);
styleHeader.setBorderTop((short)1);
styleHeader.setBorderBottom((short)1);
styleHeader.setBorderLeft((short)1);
styleHeader.setBorderRight((short)1);
HSSFCellStyle styleBody = workbook.createCellStyle();
styleBody.setBorderTop((short)1);
styleBody.setBorderBottom((short)1);
styleBody.setBorderLeft((short)1);
styleBody.setBorderRight((short)1);
styleBody.setWrapText(true);
HSSFRow row = null;
HSSFCell cell = null;
int titleRowNum = 0;
int startRowNum = 0;
if (null != dataMap.get("startRowNum"))
startRowNum = ((Integer)dataMap.get("startRowNum")).intValue() - 1;
List<List<String>> dataTitle = (List)dataMap.get("dataTitle");
int i;
if (dataTitle != null && dataTitle.size() > 0)
titleRowNum = dataTitle.size();
for(int i = 0; i < dataTitle.size(); ++i)
row = sheet.createRow(i + startRowNum);
for(i = 0; i < ((List)dataTitle.get(i)).size(); ++i)
cell = row.createCell(i);
cell.setCellValue((String)((List)dataTitle.get(i)).get(i));
cell.setCellStyle(styleHeader);
sheet.setColumnWidth(i, 6400);
List<List<String>> dataBody = (List)dataMap.get("dataBody");
if (dataBody != null && dataBody.size() > 0)
for(i = 0; i < dataBody.size(); ++i)
row = sheet.createRow(i + titleRowNum + startRowNum);
if (dataBody.get(i) != null && ((List)dataBody.get(i)).size() > 0)
for(int j = 0; j < ((List)dataBody.get(i)).size(); ++j)
cell = row.createCell(j);
cell.setCellValue((String)((List)dataBody.get(i)).get(j));
cell.setCellStyle(styleBody);
#------------------------------------
@SuppressWarnings("unchecked")
@RequestMapping("/exportDataFile")
public void exportDataFile(HttpServletRequest request, HttpServletResponse response, IbPlan ibPlan)
throws Exception
Map<Long, BsMallEntity> mallMap = bsMallService.getAllBsMallMap(null);
// 查询数据
Paginator paginator = new Paginator();
paginator.setItemsPerPage(Constants.DOWNLOAD_LIMIT);
paginator.setParams(ibPlan);
paginator = ibPlanService.getListBySelectivePage(paginator);
List<IbPlan> ibPlanList = (List<IbPlan>) paginator.getResults();
// 报表数据
Map<String, Object> dataMap = new HashMap<String, Object>();
// 文件路径
dataMap.put("filePath", servletContext.getRealPath("/") + "merchantInfo" + "-"
+ DateUtil.formatDate(new Date(), "yyyyMMdd") + ".xls");
// 模板路径
// dataMap.put("tempPath", servletContext.getRealPath("/") +
// "excelTemplate/InfoTemplate.xls");
// dataMap.put("startRowNum", 3);
// 表单名
dataMap.put("sheetName", "招商信息");
// 标题部分
List<List<String>> dataTitle = new ArrayList<List<String>>();
List<String> title = new ArrayList<String>();
title.add("品牌");
title.add("项目");
title.add("当前进度");
title.add("商务网批");
title.add("招商人员");
title.add("更新时间");
title.add("最新留言/备注");
dataTitle.add(title);
dataMap.put("dataTitle", dataTitle);
// 数据部分
List<List<String>> dataBody = new ArrayList<List<String>>();
List<String> body = null;
for (IbPlan entity : ibPlanList)
body = new ArrayList<String>();
// 品牌
body.add(entity.getBrandName());
// 项目
if (null != mallMap.get(entity.getMallId()))
body.add(mallMap.get(entity.getMallId()).getMallName());
else
body.add("");
// 当前进度
body.add(MerchantProgressEnum.getMap().get(entity.getStatus()));
// 商务网批
if (null != entity.getProgressList() && entity.getProgressList().size() >= 3)
body.add(entity.getProgressList().get(2).getNetcommentNo());
else
body.add("");
// 招商人员
body.add(entity.getManagerName());
// 更新时间
body.add(DateUtils.format(entity.getUpdatedDate(), "yyyy-MM-dd"));
// 最新留言/备注
body.add(entity.getContent());
dataBody.add(body);
dataMap.put("dataBody", dataBody);
// 报表导出
try
String filePath = FileUtils.exportExcel(dataMap);
// 获取文件
File file = new File(filePath);
// 取得文件名
String filename = file.getName();
// 以流的形式下载文件
InputStream fis = new BufferedInputStream(new FileInputStream(filePath));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
response.addHeader("Content-Length", "" + file.length());
response.setContentType("application/octet-stream;charset=UTF-8");
OutputStream output = response.getOutputStream();
output.write(buffer);
output.flush();
catch (Exception e)
e.printStackTrace();
logger.error("getDataFile---Excel导出出错-----" + e.getMessage());
以上是关于java文件下载改名的主要内容,如果未能解决你的问题,请参考以下文章
java用流实现创建文件夹, 文件改名, 文件删除, 文件复制
java自用代码(包括:新建单线程创建文件夹及文件map转为json并将json写入txt文件剪切或改名)
java基础IO流 复制键盘录入的目录,复制其中的.java文件到指定目录,指定目录中有重名,则改名 对加密文件计算字母个数