我有Spring MVC应用程序,我想将用户图像上传到resources/uploads文件夹中.显然,我想在我的网站上提供服务.但是当我试图sample.png放入resources文件夹时,仅仅是出于测试目的,网络服务器回答了"not found".我重新建立项目和图片变得可访问.我删除了图片,它仍然可以访问.我重新构建项目和服务器应答它应该("未找到").
这个奇怪的行为是什么?资源是否被构建到最终的jar文件中?这是否意味着,在重建项目之前,所有上传的用户图片都无法访问?如果是这样,我完全不应该将上传的文件放到资源文件夹中,那么我应该在哪里放置它们?为什么会这样,我该如何服务这些照片?
非常感谢你.
context.xml中:
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven>
<message-converters>
<beans:bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<beans:property name="objectMapper" ref="customObjectMapper"/>
</beans:bean>
</message-converters>
</annotation-driven>
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- mustache.java -->
<beans:bean id="viewResolver" class="org.springframework.web.servlet.view.mustache.MustacheViewResolver">
<beans:property name="cache" value="false" />
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".mustache" />
<beans:property name="templateLoader">
<beans:bean class="org.springframework.web.servlet.view.mustache.MustacheTemplateLoader" />
</beans:property>
</beans:bean>
<!-- Standard template engine -->
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<!--
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
-->
<context:component-scan base-package="com.me.myproject" />
<!-- JDBC Data Source. It is assumed you have MySQL running on localhost port 3306 with
username root and blank password. Change below if it's not the case -->
<beans:bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<beans:property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<beans:property name="url" value="jdbc:mysql://localhost:3306/myproject"/>
<beans:property name="username" value="someone"/>
<beans:property name="password" value="something"/>
<beans:property name="validationQuery" value="SELECT 1"/>
</beans:bean>
<!-- FlyWay -->
<beans:bean id="flyway" class="com.googlecode.flyway.core.Flyway" init-method="migrate">
<beans:property name="dataSource" ref="myDataSource"/>
</beans:bean>
<!-- Hibernate Session Factory -->
<beans:bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" depends-on="flyway">
<beans:property name="dataSource" ref="myDataSource"/>
<beans:property name="packagesToScan">
<beans:array>
<beans:value>com.me.myproject</beans:value>
</beans:array>
</beans:property>
<beans:property name="hibernateProperties">
<beans:value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.hbm2ddl.auto=validate
</beans:value>
</beans:property>
</beans:bean>
<!-- Hibernate Transaction Manager -->
<beans:bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<beans:property name="sessionFactory" ref="mySessionFactory"/>
</beans:bean>
<!-- Activates annotation based transaction management -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans:beans>
Run Code Online (Sandbox Code Playgroud)
web.xml中:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Run Code Online (Sandbox Code Playgroud)
Lor*_*ton 11
这是一个准备好使用的图像上传/下载控制器,完全用于此目的:
首先,我们需要通过简单的表单(admin.jsp)上传图像:
<form method="POST" action="uploadFile" enctype="multipart/form-data">
File to upload: <input type="file" name="file" >
<br />
Name: <input type="text" name="name" >
<br />
<br />
<input type="submit" value="Upload">
</form>
<c:if test="${not empty message}">
${message} <!-- here would be a message with a result of processing -->
</c:if>
Run Code Online (Sandbox Code Playgroud)现在我们需要一个控制器,它可以将图像上传到服务器并在以后的jsp页面显示它们:
package com.pizza.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
@Controller
public class FileUploadController {
private static final String PIZZA_IMAGES = "pizzaImages";
private static final String TOMCAT_HOME_PROPERTY = "catalina.home";
private static final String TOMCAT_HOME_PATH = System.getProperty(TOMCAT_HOME_PROPERTY);
private static final String PIZZA_IMAGES_PATH = TOMCAT_HOME_PATH + File.separator + PIZZA_IMAGES;
private static final File PIZZA_IMAGES_DIR = new File(PIZZA_IMAGES_PATH);
private static final String PIZZA_IMAGES_DIR_ABSOLUTE_PATH = PIZZA_IMAGES_DIR.getAbsolutePath() + File.separator;
private static final String FAILED_UPLOAD_MESSAGE = "You failed to upload [%s] because the file because %s";
private static final String SUCCESS_UPLOAD_MESSAGE = "You successfully uploaded file = [%s]";
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
public ModelAndView uploadFileHandler(@RequestParam("name") String name,
@RequestParam("file") MultipartFile file) {
ModelAndView modelAndView = new ModelAndView("admin");
if (file.isEmpty()) {
modelAndView.addObject("message", String.format(FAILED_UPLOAD_MESSAGE, name, "file is empty"));
} else {
createPizzaImagesDirIfNeeded();
modelAndView.addObject("message", createImage(name, file));
}
return modelAndView;
}
private void createPizzaImagesDirIfNeeded() {
if (!PIZZA_IMAGES_DIR.exists()) {
PIZZA_IMAGES_DIR.mkdirs();
}
}
private String createImage(String name, MultipartFile file) {
try {
File image = new File(PIZZA_IMAGES_DIR_ABSOLUTE_PATH + name);
BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(image));
stream.write(file.getBytes());
stream.close();
return String.format(SUCCESS_UPLOAD_MESSAGE, name);
} catch (Exception e) {
return String.format(FAILED_UPLOAD_MESSAGE, name, e.getMessage());
}
}
@RequestMapping(value = "/image/{imageName}")
@ResponseBody
public byte[] getImage(@PathVariable(value = "imageName") String imageName) throws IOException {
createPizzaImagesDirIfNeeded();
File serverFile = new File(PIZZA_IMAGES_DIR_ABSOLUTE_PATH + imageName + ".jpg");
return Files.readAllBytes(serverFile.toPath());
}
}
Run Code Online (Sandbox Code Playgroud)现在让我们测试一下我们的上传功能.选择图像并为其指定名称(包括扩展名).稍后(Upload按钮点击后)此图像将显示在{Tomcat.dir}/pizzaImages文件夹中:
让我们检查图像显示功能.为此,我们只需要<img>在我们需要显示图像的平台中包含一个标记(这就是Spring MVC的工作原理):
<img src="/image/11" />
PS所以你看,它很简单.
您必须将文件保存到外部文件夹中。
例如,我创建一个类似的目录/home/webappFolder/,并在其中创建一些其他子目录。例如,我存储上传数据的存储库目录..我放置 jasper 报告文件的报告子目录..等
/home/webappFolder/repo
/home/webappFolder/report
/home/webappFolder/logs
Run Code Online (Sandbox Code Playgroud)
正如您可以在问题中发布的 xml 中看到的那样:
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
Run Code Online (Sandbox Code Playgroud)
在资源中,您提供静态资源。
| 归档时间: |
|
| 查看次数: |
12815 次 |
| 最近记录: |