发布时间:2025-01-03 17:01
文件保存项目路径下
@ResponseBody
@RequestMapping(value = \"/uploadEx\")
public Result upload(HttpServletRequest request, @RequestParam(value = \"file\", required = false) MultipartFile file)
throws IOException {
Result r = new Result();
r.setMessage(\"添加成功\");
r.setStatusCode(0);
r.setSuccess(true);
System.out.println(\"执行upload\");
request.setCharacterEncoding(\"UTF-8\");
System.out.println(\"执行文件上传\");
String msgId = request.getParameter(\"msgboardId\");
System.out.println(\"msgId:\" + msgId);
if (!file.isEmpty()) {
System.out.println(\"成功获取文件\");
String fileName = file.getOriginalFilename();
String path = null;
String type = null;
type = fileName.indexOf(\".\") != -1 ? fileName.substring(fileName.lastIndexOf(\".\") + 1, fileName.length())
: null;
System.out.println(\"文件初始名称为:\" + fileName + \" 类型为:\" + type);
if (type != null) {
// if (\"GIF\".equals(type.toUpperCase()) || \"PNG\".equals(type.toUpperCase())
// || \"JPG\".equals(type.toUpperCase())) {
// 项目在容器中实际发布运行的根路径
String realPath = request.getSession().getServletContext().getRealPath(\"/\");
System.out.println(realPath);
String path_del = realPath;
// request.getSession().getServletContext().getRealPath(\"/\");
// 自定义的文件名称
String trueFileName = String.valueOf(System.currentTimeMillis()) + fileName;
// 设置存放图片文件的路径
path = realPath + \"/uploadEx/\" + trueFileName;
System.out.println(\"存放文件的路径:\" + path);
file.transferTo(new File(path));
System.out.println(\"文件成功上传到指定目录下\");
String newPath = \"../uploadEx/\" + trueFileName;
return r;
// } else {
System.out.println(\"不是我们想要的文件类型,请按要求重新上传\");
return \"error\";
// r.setMessage(\"不是我们想要的文件类型,请按要求重新上传\");
// r.setStatusCode(1);
// r.setSuccess(false);
// return r;
// }
} else {
// System.out.println(\"文件类型为空\");
// return \"error\";
r.setMessage(\"文件类型为空\");
r.setStatusCode(1);
r.setSuccess(false);
return r;
}
} else {
// System.out.println(\"没有找到相对应的文件\");
// return \"error\";
r.setMessage(\"没有找到相对应的文件\");
r.setStatusCode(1);
r.setSuccess(false);
return r;
}
}
下载项目中的excel文件
@RequestMapping(value = \"/excelOut\")
public void excelStandardTemplateOut(HttpServletRequest request,
HttpServletResponse response) throws IOException{
URL save = Thread.currentThread().getContextClassLoader().getResource(\"\");
String str = save.toString();
str=str.substring(5,str.length());
str=str.replaceAll(\"%20\", \" \");
int num = str.lastIndexOf(\"wsaicsl\");//wgbs 为项目名,应用到不同的项目中,这个需要修改!
str=str.substring(0, num+\"wsaicsl\".length());
str = str +\"/demo/synonyms_template.xlsx\";//Excel模板所在的路径。
File f = new File(str);
// 设置response参数,可以打开下载页面
response.reset();
response.setContentType(\"application/vnd.ms-excel;charset=utf-8\");
try {
response.setHeader(\"Content-Disposition\", \"attachment;filename=\"+ new String((\"synonyms_template\" + \".xlsx\").getBytes(), \"iso-8859-1\"));//下载文件的名称
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
ServletOutputStream out = response.getOutputStream();
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream(f));
bos = new BufferedOutputStream(out);
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch (final IOException e) {
throw e;
} finally {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
}
}