SpringMVC上传文件后返回文件服务器地址路径

2022-12-24,,,,

先写一个表单:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>文件上传</title>
</head>
<body>
<h2>文件上传</h2>
<form action="/fileUpload.jspx" enctype="multipart/form-data" method="post">
<table>
<tr>
<td>文件描述:</td>
<td><input type="text" name="description"></td>
</tr>
<tr>
<td>请选择文件:</td>
<td><input type="file" name="uploadFile"></td>
</tr>
<tr>
<td><input type="submit" value="上传"></td>
</tr>
</table>
</form>
</body>
</html>

action层接收文件处理:

/**
* 资源文件上传,返回一个文件服务器地址
* @author Libin
* @date 2018年1月12日 上午10:28:08
*/
@RequestMapping(value ="/fileUpload.jspx")
public void uploadFileBackAddress(HttpServletRequest request, HttpServletResponse response) {
try {
MultipartFile multipartFile = null;
if (request instanceof MultipartHttpServletRequest) {
multipartFile = ((MultipartHttpServletRequest)request).getFile("uploadFile");
}
if (null != multipartFile) {
/**
* 项目服务器地址路径
*/
String projectServerPath = request.getScheme() + "://"+request.getServerName()+":" +
request.getServerPort() + request.getContextPath() + "/upload/";
/**
* 上传文件绝对路径
*/
String path = request.getSession().getServletContext().getRealPath("/WEB-INF/upload/");
/**
* 上传文件名
*/
String fileName = makeFileName(multipartFile.getOriginalFilename()); File file = new File(path + fileName);
/**
* 判断路径是否存在,如果不存在就创建一个
*/
if (!file.getParentFile().exists()) { file.getParentFile().mkdirs();
}
/**
* 创建文件
*/
multipartFile.transferTo(new File(path + File.separator + fileName));
/**
* 返回服务器文件地址
*/
String serverFilePatn = projectServerPath + fileName; } } catch (Exception e) {
ajaxBackData = this.getAjaxBackDataExceptionStatus(e);
} }
springMVC配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd"
default-lazy-init="true">
<context:annotation-config />
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"></property>
<property name="maxUploadSize" value="10485760000"></property>
<property name="maxInMemorySize" value="40960"></property>
</bean>
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 在web.xml配置detectAllViewResolvers为false不根据ViewResolver接口全扫描,只根据viewResolver标识查找DispatcherServlet的视图解析器 -->
<bean id="defaultViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<!-- 设置前缀,即视图所在的路径 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 设置后缀,即视图的扩展名 -->
<property name="suffix" value=".jsp" />
</bean> </beans>



参考文章:http://www.cnblogs.com/klslb/p/8286762.html

需要添加的jar包:commons-fileupload-1.2.1.jar    commons-io.2.5.jar

SpringMVC上传文件后返回文件服务器地址路径的相关教程结束。

《SpringMVC上传文件后返回文件服务器地址路径.doc》

下载本文的Word格式文档,以方便收藏与打印。