SpringMVC上传文件后返回文件服务器地址路径
先写一个表单:
<%@ 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上传文件后返回文件服务器地址路径的更多相关文章
- Juploader 1.0 谷歌(chrome)浏览器中成功上传文件后返回信息异常
在项目中使用了Juploader 1.0无刷新上传文件的js组件,在IE8以上没有问题,代码如下: function InitialUploadDirectly(OnUploadFunc, butto ...
- IE浏览器上传文件后返回结果会自动弹出下载框
服务器使用的是node,其它语言的后台没测试过. 在IE低版本浏览器下,当你上传一个文件后后台会返回一些数据,但是IE浏览器会弹出下载提示. 这个问题是之前处理的了,没有细究,今天有人问到这个问题,顺 ...
- koa2图片上传成功后返回服务器地址,实时显示服务器图片
版本:node(8.5.0); koa(2.4.1); koa-router(7.3.0); koa-body(2.5.0); koa-static(4.0.2); 代码实现 const fs = r ...
- nss_12 上传文件后返回jsonresult结果,IE中出现文件下载框
因为控制器返回的是JsonResult, 但是在IE8中一直返回文件下载的对话框. 转到谷歌浏览器倒没有问题. 网上找的方法, 要么是跟到一个新的成功页面, 要么是直接返回html, 觉得应该有更好的 ...
- springmvc上传文件,抄别人的
SpringMVC中的文件上传 分类: SpringMVC 2012-05-17 12:55 26426人阅读 评论(13) 收藏 举报 stringuserinputclassencoding 这是 ...
- SpringMVC 上传文件 MultipartFile 转为 File
在使用 SpringMVC 上传文件时,接收到的文件格式为 MultipartFile,但是在很多场景下使用都需要File格式的文件,记录下以便日后使用. 以下mFile为MultipartFile文 ...
- FTP主动模式上传文件时返回"ftp: accept: Resource temporarily unavailable"
FTP主动模式上传文件时返回 Passive mode off ftp: accept: Resource temporarily unavailable 这个问题要从ftp的2种模式说起 PORT ...
- 2. SpringMVC 上传文件操作
1.创建java web项目:SpringMVCUploadDownFile 2.在项目的WebRoot下的WEB-INF的lib包下添加如下jar文件 com.springsource.com.mc ...
- ifrem上传文件后显示
ifrem上传文件后显示 1.上传文件按钮 <a class="btn btn-primary pull-right" id="data-upload&quo ...
随机推荐
- Linux从入门到适应(一):VSCode C++环境配置
作为在Windows环境下习惯使用Visual Studio IDE的人,对于Linux环境下的Vim编辑使用十分难受,虽然网上很多人说vim非常牛逼和强大,但是我更加习惯于使用VS code的界面, ...
- 安装composer及切换镜像为国内镜像
一.下载composer php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" 下面的 ...
- 2017 计蒜之道 初赛 第一场 B阿里天池的新任务(简单)
题链:"https://nanti.jisuanke.com/t/15500" 本来希望通过找循环节然后套KMP来通过后面题的,可是只过了B题,可能循环节不一定是存在的. #inc ...
- 17-看图理解数据结构与算法系列(NoSQL存储-LSM树)
关于LSM树 LSM树,即日志结构合并树(Log-Structured Merge-Tree).其实它并不属于一个具体的数据结构,它更多是一种数据结构的设计思想.大多NoSQL数据库核心思想都是基于L ...
- C语言基础--自加自减
有如下代码: unsigned int temp1,temp2, i=5,j=5; temp1=i++; temp2=++j; 结果是 temp1=5,temp2=6: i=6,j=6: 版权声明:本 ...
- QueryParser
[概述] 其他工具类使用比较方便,但不够灵活.QueryParser也实现了较多的匹配方式. [QueryParser的应用] /** * 使用QueryParser进行查询 * @throws Pa ...
- 56. spring boot中使用@Async实现异步调用【从零开始学Spring Boot】
什么是"异步调用"? "异步调用"对应的是"同步调用",同步调用指程序按照定义顺序依次执行,每一行程序都必须等待上一行程序执行完成之后才能执 ...
- 用mycat做读写分离:基于 MySQL主从复制
版权声明:本文为博主原创文章,未经博主允许不得转载. mycat是最近很火的一款国人发明的分布式数据库中间件,它是基于阿里的cobar的基础上进行开发的 搭建之前我们先要配置MySQL的主从复制,这个 ...
- MT6755 平台手机皮套驱动实现
是自己写注册一个input device,模仿keypad,在对应的中断处理函数中上报power key的键值. 具体实现代码如下: 在 alps/kernel-3.10/drivers/misc/m ...
- js Date()日期函数浏览器兼容问题解决方法
一般 直接new Date() 是不会出现兼容性问题的,而 new Date(datetimeformatstring) 常常会出现浏览器兼容性问题,为什么,datetimeformatstring中 ...