springmvc上传文件踩过的坑

 1     @RequestMapping("/addTweet")
 2     public String addTweet(TweetVO tweetVO, HttpServletRequest request, Model model,
 3                            @RequestParam(value = "file", required = false) MultipartFile file) {
 4         try {
 5             String pathRoot = String.valueOf(request.getSession().getServletContext().getRealPath("/"));
 6             String path = "";
 7             if (!file.isEmpty()) {
 8                 // 生成uuid作为文件名称
 9                 String uuid = UUID.randomUUID().toString().replaceAll("-", "");
10                 // 获得文件类型(可以判断如果不是某种类型,禁止上传)
11                 String contentType = file.getContentType();
12                 // 获得文件后缀名称
13                 String imageName = contentType.substring(contentType.indexOf("/") + 1);
14                 path = "/static/image/" + uuid + "." + imageName;
15                 file.transferTo(new File(pathRoot + path));
16                 request.setAttribute("imagesPath", path);
17 //                model.addAttribute("imagesPath", path);
18                 tweetVO.setPicture(path);
19                 tweetService.addTweet(tweetVO);
20             }
21         } catch (Exception e) {
22             e.printStackTrace();
23         }
24         return "redirect:/tweet/list";
25     }

在transferTo中,使用的是绝对路径 pathRoot + path,但是在数据库中存储的时候,使用的是相对路径 path

这样,在页面显示的时候,就可以使用

<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() +
            ":" + request.getServerPort() + path + "/";
    request.setAttribute("path", path);
    request.setAttribute("basePath", basePath);
%>
<img src="${basePath}${tweet.key.picture}" alt="${basePath}${tweet.key.picture}"/>

其中basePath表示的是服务器路径http://localhost:8080/bignews1/ ,tweet.key.picture表示的是之前的数据库里面存储的相对路径,这样就能显示出图片了。

注意,不能在数据库中直接存储绝对路径,例如c:\\windows\\system32这种,如果在网页里面直接请求这个路径,会报错(not allowed to load local resource)

时间: 2024-12-24 18:12:41

springmvc上传文件踩过的坑的相关文章

springmvc上传文件 踩过的坑

spring-root.xml中配置 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="9000000"/> <property name="maxInM

SpringMVC上传文件的三种解析方式

springMVC上传文件后,在action解析file文件的三种方式. jsp页面的写法: <form action="parserUploadFile1" method="post" enctype="multipart/form-data"> <input type="file" name="file" /> <input type="submit"

2. SpringMVC 上传文件操作

1.创建java web项目:SpringMVCUploadDownFile 2.在项目的WebRoot下的WEB-INF的lib包下添加如下jar文件 1 com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar 2 com.springsource.net.sf.cglib-2.2.0.jar 3 com.springsource.org.aopalliance-1.0.0.jar 4 com.springsource.org.apache.commo

springmvc 上传文件时的错误

使用springmvc上传文件一直失败,文件参数一直为null, 原来是配置文件没写成功. <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 最大上传尺寸 B单位 1M= 1024*1024 --> <property name="maxUploadSize&

springmvc上传文件,抄别人的

SpringMVC中的文件上传 分类: SpringMVC 2012-05-17 12:55 26426人阅读 评论(13) 收藏 举报 stringuserinputclassencoding 这是用的是SpringMVC-3.1.1.commons-fileupload-1.2.2和io-2.0.1 首先是web.xml [html] view plaincopyprint? <?xml version="1.0" encoding="UTF-8"?>

SpringMVC上传文件总结

如果是maven项目 需要在pom.xml文件里面引入下面两个jar包 <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>com

springmvc 上传文件的问题

今天用springmvc 上传文件的时候 报错 org.apache.catalina.connector.RequestFacade cannot be cast to org.springframework.web.multipart.MultipartHttpServletRequest 网上查找原因 说的有如下几种: 1.表单form 上没有 enctype="multipart/form-data"   这个属性 2.配置文件: <bean id="multi

SpringMVC上传文件(图片)并保存到本地

SpringMVC上传文件(图片)并保存到本地 小记一波~ 基本的MVC配置就不展示了,这里给出核心代码 在spring-mvc的配置文件中写入如下配置 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize">

SpringMVC上传文件的三种方式(转载)

直接上代码吧,大伙一看便知 这时:commonsmultipartresolver 的源码,可以研究一下 http://www.verysource.com/code/2337329_1/commonsmultipartresolver.java.html 前台: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 <%@ page language="java" contentTy