spring-servlet.xml

 <!-- SpringMVC上传文件时,需要配置MultipartResolver处理器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
3 <property name="defaultEncoding" value="UTF-8" />
4 <!-- 指定所上传文件的总大小,单位字节。注意maxUploadSize属性的限制不是针对单个文件,而是所有文件的容量之和 -->
5 <property name="maxUploadSize" value="10240000" />
6 </bean>

upload/index.jsp

 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>单图片上传</title>
</head>
<body>
<fieldset>
<legend>图片上传</legend>
<h2>只能上传单张10M以下的 PNG、JPG、GIF 格式的图片</h2>
<form action="/shop/auth/photoUpload" method="post" enctype="multipart/form-data">
选择文件:<input type="file" name="file">
<input type="submit" value="上传">
</form>
</fieldset>
</body>
</html>

或者使用ExtJs

js/user/photoUpload.js

 Ext.onReady(function(){
Ext.create('Ext.form.Panel', {
title: '图片上传',
width: 600,
bodyPadding: 10,
frame: true,
renderTo: Ext.getBody(),
items: [{
9 xtype: 'filefield',
name: 'file',
fieldLabel: 'Photo',
labelWidth: 50,
msgTarget: 'side',
fileUpload: true ,
allowBlank: false,
16 blankText:"Select an image",
emptyText: 'You can only upload a single PNG 10M or less, JPG, GIF format images',
anchor: '100%',
buttonText: '选择图片'
}], buttons: [{
text: '上传',
handler: function() {
var form = this.up('form').getForm();
if(form.isValid()){
form.submit({
url: '/shop/auth/photoUpload',
waitMsg: '正在上传图片...',
success: function(fp, o) {
Ext.Msg.alert('提示', o.result.msg);
}
});
}
}
}]
});
});

pages/user/photoUpload.html

 <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>图片上传</title>
</head>
<link href="../../ext-4.2.1.883/resources/css/ext-all.css" rel="stylesheet"
type="text/css" />
<script type="text/javascript" src="../../ext-4.2.1.883/ext-all.js"></script>
<script src="../../js/user/photoUpload.js" type="text/javascript"></script>
<body> </body>
</html>  

AuthController.java

/**
* 图片文件上传
*/
@ResponseBody
@RequestMapping(value = "/photoUpload", method = RequestMethod.POST)
public ResultData<Object> photoUpload(MultipartFile file, HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IllegalStateException, IOException {
ResultData<Object> resultData = new ResultData<>();
// 判断用户是否登录
/*User user=(User) session.getAttribute("user");
if (user==null) {
resultData.setCode(40029);
resultData.setMsg("用户未登录");
return resultData;
}*/
if (file != null) {// 判断上传的文件是否为空
String path = null;// 文件路径
String type = null;// 文件类型
String fileName = file.getOriginalFilename();// 文件原名称
System.out.println("上传的文件原名称:" + fileName); // 判断文件类型
type = fileName.indexOf(".") != -1 ? fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()) : null;
if (type != null) {// 判断文件类型是否为空
if ("GIF".equals(type.toUpperCase()) || "PNG".equals(type.toUpperCase()) || "JPG".equals(type.toUpperCase())) { // 项目在容器中实际发布运行的根路径
String realPath = request.getSession().getServletContext().getRealPath("/"); // 自定义的文件名称
String trueFileName = String.valueOf(System.currentTimeMillis()) + fileName; // 设置存放图片文件的路径
path = realPath +/*System.getProperty("file.separator")+*/trueFileName;
System.out.println("存放图片文件的路径:" + path); // 转存文件到指定的路径
file.transferTo(new File(path));
System.out.println("文件成功上传到指定目录下");
} else {
System.out.println("不是我们想要的文件类型,请按要求重新上传");
return null;
}
} else {
System.out.println("文件类型为空");
return null;
}
} else {
System.out.println("没有找到相对应的文件");
return null;
}
return resultData;
}

ResultData.java 代码如下:

public class ResultData<T> {

    private T data;

    private int code = 200;

    private String msg;

    private Boolean success = true;

    public Boolean getSuccess() {
return success;
} public void setSuccess(Boolean success) {
this.success = success;
} public T getData() {
return data;
} public void setData(T data) {
this.data = data;
} public int getCode() { return code;
} public void setCode(int code) {
if (200 != code) {
success = false;
}
this.code = code;
} public String getMsg() {
return msg;
} public void setMsg(String msg) {
this.msg = msg;
} }

SpringMvc MultipartFile 图片文件上传的更多相关文章

  1. SpringMvc commons-fileupload图片/文件上传

    简介 SpringMvc文件上传的实现,是由commons-fileupload这个jar包实现的. 需求 在修改商品页面,添加上传商品图片功能. Maven依赖包 pom.xml <!-- 文 ...

  2. springmvc图片文件上传接口

    springmvc图片文件上传 用MultipartFile文件方式传输 Controller package com.controller; import java.awt.image.Buffer ...

  3. SpringMVC实现ajax文件上传

    SpringMVC实现文件上传,直接上代码: 后台代码: 01 @RequestMapping(value = "/uploadApk") 02 @ResponseBody 03 ...

  4. (转)SpringMVC学习(九)——SpringMVC中实现文件上传

    http://blog.csdn.net/yerenyuan_pku/article/details/72511975 这一篇博文主要来总结下SpringMVC中实现文件上传的步骤.但这里我只讲单个文 ...

  5. 使用SpringMVC框架实现文件上传和下载功能

    使用SpringMVC框架实现文件上传和下载功能 (一)单个文件上传 ①配置文件上传解释器 <!—配置文件上传解释器 --> <mvc:annotation-driven>&l ...

  6. 利用spring的MultipartFile实现文件上传【原】

    利用spring的MultipartFile实现文件上传 主要依赖jar包 spring-web-3.0.6.RELEASE.jar 用到 (org.springframework.web.multi ...

  7. .Net Core 图片文件上传下载

    当下.Net Core项目可是如雨后春笋一般发展起来,作为.Net大军中的一员,我热忱地拥抱了.Net Core并且积极使用其进行业务的开发,我们先介绍下.Net Core项目下实现文件上传下载接口. ...

  8. SpringMVC 使用MultipartFile实现文件上传(转)

    http://blog.csdn.net/kouwoo/article/details/40507565 一.配置文件:SpringMVC 用的是 的MultipartFile来进行文件上传 所以我们 ...

  9. SpringMVC中使用 MultipartFile 进行文件上传下载及删除

    一:引入必要的包 <!--文件上传--> <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fil ...

随机推荐

  1. java 多线程 一个博客

    http://blog.csdn.net/a352193394/article/category/2563875 Java多线程之~~~线程安全容器的非阻塞容器 在并发编程中,会经常遇到使用容器.但是 ...

  2. JavaScript性能优化技巧之函数节流

    函数节流技术的主要思路是,通过一个定时器,阻断连续重复的函数调用.对于我们自己内部使用的函数,这通常意义不大,也不推荐使用这个技术,它可能会丢失对某些数据的处理.但是对于在用户界面调用的函数,却非常有 ...

  3. ios控件 UIViewController

    //通过xib文件创建一个视图控制器.并作为窗口的根控制器 self.viewController = [[ViewController alloc] initWithNibName:@"V ...

  4. linuxmint更改权限

    sudo chmod -R 777 要更改的目录或文件

  5. 多项目中SVN权限管理精辟解析

    本节和大家讨论一下多项目SVN权限管理,主要包括建立版本库,修改版本库配置文件,配置允许访问的用户,设置用户访问权限.下面我们就来看一下SVN权限管理.svn权限管理svn的权限管理涉及到一下文件:p ...

  6. LinuxIP地址、网卡相关、克隆、VM

    改IP地址(#setup) 1.输入vi /etc/sysconfig/network-scripts/ifcfg-eth0 2.里面的内容修改为 DEVICE=eth0HWADDR=FC:4D:D4 ...

  7. 2016 ccpc 杭州赛区的总结

    毕竟是在杭电比的,和之前大连的icpc不同,杭电毕竟是隔壁学校,来回吃住全都是在自家寝室,方便! 不过说到方便也是有点不方便,室友都喜欢玩游戏,即使我昨晚9.30就睡觉了,仍然是凌晨一点才睡着,233 ...

  8. IOS传值之Block传值(二)

    @interface QWViewController : UIViewController @property(nonatomic,strong)UILabel *label; @property( ...

  9. 笨方法学python--数字和数学计算

    1 数学运算符号 + plus 加号 - minus 减号 / slash 除法 * asterisk 乘法 % percent 模除 求余 < less than 小于号 > great ...

  10. ios中关于UIImagePickerController的一些知识总结

    记得添加MobileCoreServices.framework 及导入#import <MobileCoreServices/MobileCoreServices.h> @interfa ...