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 ...
随机推荐
- SQLServer锁的概述
SQLServer锁的概述 锁的概述 一. 为什么要引入锁 多个用户同时对数据库的并发操作时会带来以下数据不一致的问题: 丢失更新 A,B两个用户读同一数据并进行修改,其中一个用户的修改结果破坏了 ...
- Redux的中间件Middleware不难,我信了^_^
Redux的action和reducer已经足够复杂了,现在还需要理解Redux的中间件.为什么Redux的存在有何意义?为什么Redux的中间件有这么多层的函数返回?Redux的中间件究竟是如何工作 ...
- gulp 前缀
var gulp = require('gulp'), autoprefixer = require('gulp-autoprefixer'); gulp.task('testAutoFx', fun ...
- Java使用JNA方式调用DLL(动态链接库)(原创,装载请注明出处)
Java使用JNA调用DLL 1.准备 1.JDK环境 2.Eclipse 3.JNA包 下载JNA包: (1).JNA的Github:https://github.com/java-native-a ...
- 笔试算法题(10):深度优先,广度优先以及层序遍历 & 第一个仅出现一次的字符
出题:要求实现层序遍历二元搜索树,并对比BFS与DFS的区别 分析:层序遍历也就是由上至下,从左到右的遍历每一层的节点,类似于BFS的策略,使用Queue可以实现,BFS不能用递归实现(由于每一层都需 ...
- 简述HTTP报文请求方法和状态响应码
1. Method 请求方法,表明客户端希望服务器对资源执行的动作: 1.1 GET 向服务器请求资源. 1.2 HEAD 和GET方法的行为类似,但服务器在响应中只返回首部,不会返回实体的主体部分. ...
- python3.x Day6 多进程
多进程:1.每个子进程申请到的资源都是独立的,不与其他进程共享.2.语法上和线程基本上差不多,使用multiprocessing.Process(target=xxxx,args=(xxx,xxx,x ...
- JavaScript中整型数据使用
JavaScript中整型数据使用 制作人:全心全意 JavaScript的数字格式允许精确地表示-900719925474092(-253)和900719925474092(253)之间的所有整数, ...
- NodeJs中数据库的使用
另一遍通用的NODEJS数据库方法koa,express,node 通用方法连接MySQL 1.Node.js 连接 MySQL $ cnpm install mysql 连接mysql: var m ...
- UVa 514 铁轨
题意: #include <bits/stdc++.h> using namespace std; int main() { int n; ]; ; ) { ]) && n ...