多语言展示
当前在线:429今日阅读:126今日分享:42

java中文件上传

上传文件到E盘然后读取;以图片为例
工具/原料

myeclipse

方法/步骤
1

引入maven依赖;    commons-io    commons-io    2.4               commons-fileupload              commons-fileupload              1.3.1          将此段依赖写入pom.xml文件中

2

@RequestMapping(value = 'index.do',method=RequestMethod.GET) public String uploadIndex(){ return 'upload/index'; }上传文件首页的后台

方法/步骤2
1

@RequestMapping(value = 'index.do',method = RequestMethod.POST) public String upload(HttpServletRequest request,MultipartFile file,Model model){ String fileName = file.getOriginalFilename(); String path = 'E:/image/'; System.out.println(path); File dest = new File(path+fileName);

2

try {file.transferTo(dest);model.addAttribute('success', true);model.addAttribute('imageName', fileName);model.addAttribute('message','文件上传成功');} catch (IllegalStateException | IOException e) {// TODO Auto-generated catch blockmodel.addAttribute('success', false);model.addAttribute('message','文件上传失败');e.printStackTrace();}return 'upload/uploadSuccess';}上传文件,将文件写入到对应的文件夹,

3

<%@ page contentType='text/html;charset=UTF-8' language='java' %><%@taglib prefix='c' uri='http://java.sun.com/jsp/jstl/core' %>            图片上传                    ${message }          

4

tomcat的server.xml配置              路径不能错;

注意事项

String path = 'E:/image/'; /斜杠不能丢

推荐信息