*文件上传:*
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
MultipartFile multipartFile = multipartRequest.getFile(“inputFile”);读取文件原始位置
String date=new SimpleDateFormat(“yyyyMMddHHmmss”).format(Calendar.getInstance().getTime());
String sourceName =date+multipartFile.getOriginalFilename(); // 原始文件名
String fileType = sourceName.substring(sourceName.lastIndexOf(“.”));
System.out.println(fileType);
System.out.println(“上传的文件名为:”+sourceName+”类型为:”+fileType);
//String base = request.getSession().getServletContext().getRealPath(“/”) + “attachments” + File.separator + “uploadedExcel”;
String base = ConfigureFileHelper.getProperty(“manageruser_upload_dirs”);
File file = new File(base);
if(!file.exists()){
file.mkdirs();
}
String path=base + File.separator + sourceName;
multipartFile.transferTo(new File(path));
System.out.println(“路径是”+path);
文件下载:
String filePath = ConfigureFileHelper.getProperty(“manageruser_download_dirs”); //文件在项目中的路径
File outfile = new File(filePath);
String filename = outfile.getName();// 获取文件名称
InputStream fis = new BufferedInputStream(new FileInputStream(
filePath));
byte[] buffer = new byte[fis.available()];
fis.read(buffer); //读取文件流
fis.close();
//response.reset(); //重置结果集
response.setContentType(“application/octet-stream”); //设置数据种类
/*response.addHeader(“Content-Disposition”, “attachment;filename=”
+ new String(“员工名单文件模板.xls”.getBytes(“utf-8”),
“iso8859-1”)); //返回头 文件名
*/ response.setHeader(“Content-Disposition”, “attachment; filename=\””
+ URLEncoder.encode(“员工名单文件模板.xls”, “UTF-8”));
//response.addHeader(“Content-Length”, “” + outfile.length()); //返回头 文件大小
//获取返回体输出权
OutputStream os = new BufferedOutputStream(response.getOutputStream());
os.write(buffer); // 输出文件
os.flush();
os.close();