1.文件上传到服务器指定位置
String uploadFileName = file.getOriginalFilename();
String extendName = uploadFileName.substring(uploadFileName.lastIndexOf(".") + 1, uploadFileName.length());
String uuid = UUID.randomUUID().toString().replace("-", "").toUpperCase();
String newFileName = uuid + "." + extendName;
String filePath = request.getServletContext().getRealPath("/docupload") + "/" + newFileName;
file.transferTo(new File(filePath));
System.out.println("文件上传结束!");
2.从服务器指定路径中下载文件到个人目录下
String path = request.getSession().getServletContext().getRealPath("/docupload");
File file = new File(path, fileName);
if (file.exists()) {
try {
HttpHeaders headers = new HttpHeaders();
String newFileName = document.getTitle() + "." + fileName.split("\\.")[1];
String downName = new String(newFileName.getBytes("UTF-8"), "ISO-8859-1");
headers.setContentDispositionFormData("attachment", downName);
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
ResponseEntity<byte[]> responseEntity = new ResponseEntity<>(FileUtils.readFileToByteArray(file), headers, HttpStatus.OK);
return responseEntity;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
3.java制作Excel表格,并通过流形式下载
List<Users> users = usersService.list(queryWrapper);
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("用户信息表");
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 5));
HSSFRow bTitleRow = sheet.createRow(0);
HSSFCell bTitleCell = bTitleRow.createCell(0);
bTitleCell.setCellValue("用户信息表");
HSSFCellStyle style = workbook.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);
HSSFFont hssfFont = workbook.createFont();
hssfFont.setFontName("微软雅黑");
hssfFont.setColor((short) 6);
style.setFont(hssfFont);
bTitleCell.setCellStyle(style);
HSSFRow titleRow = sheet.createRow(1);
String[] titles = {"编号", "登录名", "用户名", "状态", "创建时间", "更新时间"};
for (int i = 0; i < titles.length; i++) {
HSSFCell cell = titleRow.createCell(i);
cell.setCellValue(titles[i]);
}
for (int i = 0; i < users.size(); i++) {
Users user = users.get(i);
HSSFRow row = sheet.createRow(i + 2);
HSSFCell cell_id = row.createCell(0);
cell_id.setCellValue(user.getId());
HSSFCell cell_login_name = row.createCell(1);
cell_login_name.setCellValue(user.getLogin_name());
HSSFCell cell_username = row.createCell(2);
cell_username.setCellValue(user.getUsername());
HSSFCell cell_status = row.createCell(3);
cell_status.setCellValue(user.getStatus() == 1 ? "启动" : "禁用");
HSSFCell cell_create_date = row.createCell(4);
cell_create_date.setCellValue(user.getCreate_date());
HSSFCell cell_update_date = row.createCell(5);
cell_update_date.setCellValue(user.getUpdate_date());
}
String fileName = "用户信息表.xls";
response.setContentType("application/vnd.ms-excel");
String contentDisposition = "";
if (request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0) {
contentDisposition = "attachment; filename=\"" + new String(fileName.getBytes("UTF-8"), "ISO8859-1") + "\"";
} else {
contentDisposition = "attachment; filename=\"" + URLEncoder.encode(fileName, "UTF-8") + "\"";
}
response.setHeader("Content-Disposition", contentDisposition);
response.setCharacterEncoding("UTF-8");
ServletOutputStream outputStream = response.getOutputStream();
try {
workbook.write(outputStream);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
System.out.println("Excel文件生成完毕!");