java文件上传与下载
Posted 陈陈chenchen
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java文件上传与下载相关的知识,希望对你有一定的参考价值。
不多说,直接上实例代码!!!
我使用的是idea工具,maven管理。
首先,在pom.xml引入上传文件所需要的包。
<!--fileupload--> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.2</version> </dependency>
单文件上传:
/** * Created by 陈 on 2017/9/17. */ @Controller @RequestMapping("/home") public class UploadController { @RequestMapping("/oneUpload") public String upload(@RequestParam("file") MultipartFile file, HttpServletRequest request){ String uploadUrl = request.getSession().getServletContext().getRealPath("/") + "upload/"; String filename = file.getOriginalFilename(); //判断相应路径是否存在,不存在则创建 File dir = new File(uploadUrl); if(!dir.exists()){ dir.mkdir(); } System.out.println("文件上传到:" + uploadUrl + filename); File targetFile = new File(uploadUrl + filename); if(!targetFile.exists()){ try { targetFile.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } //移动上传文件 try { file.transferTo(targetFile); } catch (IOException e) { e.printStackTrace(); } System.out.println("http://127.0.0.1:8080/study2/upload/" + filename); return null; }
}
多文件上传:
@RequestMapping("/moreUpload") public String moreUpload(HttpServletRequest request){ MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request; Map<String, MultipartFile> files = multipartHttpServletRequest.getFileMap(); String uploadUrl = request.getSession().getServletContext().getRealPath("/") + "upload/"; //判断相应路径是否存在,不存在则创建 File dir = new File(uploadUrl); if(!dir.exists()){ dir.mkdir(); } List<String> fileList = new ArrayList<String>(); for(MultipartFile file : files.values()){ File targetFile = new File(uploadUrl + file.getOriginalFilename()); if(!targetFile.exists()){ try { targetFile.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { file.transferTo(targetFile); fileList.add("http://localhost:8080/study2/upload/" + file.getOriginalFilename()); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } request.setAttribute("files",fileList); System.out.println(files.size()); return null; }
文件下载:
@Controller public class downLoadController { @RequestMapping("/downLoad") public String downLoad(String fileName, HttpServletRequest request, HttpServletResponse response){ response.setContentType("text/html;charset=utf-8"); try { request.setCharacterEncoding("UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } BufferedInputStream bis = null; BufferedOutputStream bos = null; String ctxPath = request.getSession().getServletContext().getRealPath("/") + "upload/"; String downLoadPath = ctxPath + fileName; System.out.println(downLoadPath); try { long fileLength = new java.io.File(downLoadPath).length(); response.setContentType("application/x-msdownload;"); response.setHeader("Content-disposition", "attachment; filename=" + new String(fileName.getBytes("utf-8"), "ISO8859-1")); response.setHeader("Content-Length", String.valueOf(fileLength)); bis = new BufferedInputStream(new FileInputStream(downLoadPath)); bos = new BufferedOutputStream(response.getOutputStream()); byte[] buff = new byte[2048]; int bytesRead; while ((bytesRead = bis.read(buff, 0, buff.length)) != -1) { bos.write(buff, 0, bytesRead); } }catch (Exception e){ e.printStackTrace(); }finally { try { bis.close(); } catch (IOException e) { e.printStackTrace(); } try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } return null; } }
以上是关于java文件上传与下载的主要内容,如果未能解决你的问题,请参考以下文章