文件上传下载
程序员文章站
2022-05-29 10:51:41
单文件上传 @PostMapping(value = "upload") @ResponseBody public Map upload(@RequestParam("file") MultipartFile file) { Map map=new HashMap(); try { if (file ......
单文件上传
<form action="/layui/upload" method="post" enctype="multipart/form-data">
文件:<input type="file" name="file"/>
<input type="submit"/>
</form>
@postmapping(value = "upload") @responsebody public map upload(@requestparam("file") multipartfile file) { map map=new hashmap(); try { if (file.isempty()) { map.put("msg","文件为空"); return map; } // 获取文件名 string filename = file.getoriginalfilename(); log.info("上传的文件名为:" + filename); // 获取文件的后缀名 string suffixname = filename.substring(filename.lastindexof(".")); log.info("文件的后缀名为:" + suffixname); // 设置文件存储路径 string filepath = "d:/测试文件上传下载功能/"; string path = filepath + filename; file dest = new file(path); // 检测是否存在目录 if (!dest.getparentfile().exists()) { dest.getparentfile().mkdirs();// 新建文件夹 } file.transferto(dest);// 文件写入 map.put("msg","上传成功"); return map; } catch (illegalstateexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } map.put("msg","上传失败"); return map; }
多文件上传
<p>多文件上传</p> <form method="post" enctype="multipart/form-data" action="/layui/batch"> <p>文件1:<input type="file" name="file"/></p> <p>文件2:<input type="file" name="file"/></p> <p><input type="submit" value="上传"/></p> </form>
@postmapping("batch") @responsebody public string handlefileupload(httpservletrequest request) { list<multipartfile> files = ((multiparthttpservletrequest) request).getfiles("file"); multipartfile file = null; bufferedoutputstream stream = null; for (int i = 0; i < files.size(); ++i) { file = files.get(i); string filepath = "d:/测试文件上传下载功能/"; if (!file.isempty()) { try { byte[] bytes = file.getbytes(); stream = new bufferedoutputstream(new fileoutputstream( new file(filepath + file.getoriginalfilename())));//设置文件路径及名字 stream.write(bytes);// 写入 stream.close(); } catch (exception e) { stream = null; return "第 " + i + " 个文件上传失败 ==> " + e.getmessage(); } } else { return "第 " + i + " 个文件上传失败因为文件为空"; } } return "上传成功"; }
文件下载
<p>文件下载</p> <a href="/layui/download">下载文件</a>
@getmapping("download") @responsebody public string downloadfile(httpservletrequest request, httpservletresponse response) { string filename = "交接内容.xlsx";// 文件名 if (filename != null) { //设置文件路径 file file = new file("c:/users/fanyx/desktop/交接内容.xlsx"); //file file = new file(realpath , filename); if (file.exists()) { byte[] buffer = new byte[1024]; fileinputstream fis = null; bufferedinputstream bis = null; try { response.setcontenttype("application/force-download");// 设置强制下载不打开 response.addheader("content-disposition", "attachment;filename=" + new string(filename.getbytes("utf-8"),"iso8859-1"));// 设置文件名 fis = new fileinputstream(file); bis = new bufferedinputstream(fis); outputstream os = response.getoutputstream(); int i = bis.read(buffer); while (i != -1) { os.write(buffer, 0, i); i = bis.read(buffer); } return "下载成功"; } catch (exception e) { e.printstacktrace(); } finally { if (bis != null) { try { bis.close(); } catch (ioexception e) { e.printstacktrace(); } } if (fis != null) { try { fis.close(); } catch (ioexception e) { e.printstacktrace(); } } } } } return "下载失败"; }
上一篇: Linux组管理(6)