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 ...
随机推荐
- PHP 下基于 php-amqp 扩展的 RabbitMQ 简单用例 (三) -- Header Exchange
此模式下,消息的routing key 和队列的 routing key 会被完全忽略,而是在交换机推送消息和队列绑定交换机时, 分别为消息和队列设置 headers 属性, 通过匹配消息和队列的 h ...
- org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException 前言中不允许有内容 来自类路径资源的XML文档中的第1行是无效的
今天复习一下Spring和Hibernate的整合,遇到了一个问题,报错信息如下: org.springframework.beans.factory.xml.XmlBeanDefinitionSto ...
- 洛谷——P2417 课程
P2417 课程 裸地匈牙利算法, 贪心的不断匹配,若没匹配,则匹配:反之,判断与之匹配的点能否在找另一个点匹配,若能,抢多这个点与之匹配 时间复杂度$O(n\times m)$ #include&l ...
- MapReduce实例——查询缺失扑克牌
问题: 解决: 首先分为两个过程,Map过程将<=10的牌去掉,然后只针对于>10的牌进行分类,Reduce过程,将Map传过来的键值对进行统计,然后计算出少于3张牌的的花色 1.代码 1 ...
- VSCODE插件开发:用户输入输出
阅读这篇文章之前,假设你已经具有开发helloworld的插件的能力. vscode.window 简介 vscode.window 负责当前激活窗口的输入输出,比如展示信息,和用户输入等功能都是用v ...
- leds-gpio driver
我们还是先看看platform device是如何define的 platform device 是如何定义的 example1 在板级驱动中定义, 通过platform_add_devices()函 ...
- stl sort和qsort的使用
好不容易使用了下stl的qsort函数,顺便和sort函数一起总结下: 很多时候我们都需要用到排序. 例如: 1 #include <iostream> #include <algo ...
- 59. Spring Boot Validator校验【从零开始学Spring Boot】
大纲: (1) 入门例子: (2) 国际化: (3) 在代码中添加错误信息: (1) 入门例子: Validator主要是校验用户提交的数据的合理性的,比如是否为空了,密码长度是否大于6位,是否是纯数 ...
- [luoguP1044] 栈(数论?)
传送门 卡特兰数 代码 #include <cstdio> int n; long long f[20]; int main() { int i; scanf("%d" ...
- Java 学习(5):修饰符 运算符
目录 --- 修饰符 --- 运算符 --- 循环结构 --- 分支结构 修饰符: 修饰符用来定义类.方法或者变量,通常放在语句的最前端.如下: public class className { // ...