一、同步上传文件

  • 导入common-fileupload这个jar包。

  • 配置     springmvc-servlet.xml
<?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: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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> <!-- 激活@Required @Autowired @Resource等标注-->
<context:annotation-config></context:annotation-config> <!-- DispatcherServlet上下文,扫描base-package包中的类,并自动加载到spring容器中 -->
<context:component-scan base-package="com.shyroke.controller">
</context:component-scan> <!-- 启用@Component,@Controller,@Service,@Repository注解驱动 -->
<mvc:annotation-driven/> <mvc:default-servlet-handler /> <!-- 文件处理 -->
<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> <!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
  • index.jsp
<body>

    <form method="post" id="file_form1" action="<%=request.getContextPath()%>/uploadController/uploadForm"
enctype="multipart/form-data">
<input type="text" name="dogid" id="dogid" value="用户名"/>
<input type="text" name="dogname" id="dogname" value="复选框1"/>
<input type="file" name="faceimage" id="faceimage" size="40"/>
<input type="submit" value="上传文件"/>
</form>
</body>
  • controller
package com.shyroke.controller;

import java.io.File;
import java.io.InputStream; import javax.servlet.ServletContext; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile; import com.shyroke.bean.DogBean; @Controller
@RequestMapping(value="/uploadController")
public class UploadController { @Autowired
private ServletContext context; @RequestMapping(value = "/uploadForm")
public String uploadFile(
DogBean dogBean,
@RequestParam(name = "faceimage", required = false) MultipartFile faceimage)
throws Exception { System.out.println(dogBean); System.out.println(faceimage); String dogid = dogBean.getDogid();
String dogname = dogBean.getDogname(); String name = faceimage.getName();
String filename = faceimage.getOriginalFilename();
InputStream inputStream = faceimage.getInputStream();
/**
* 输出控件的名称,faceimage
*/
System.out.println("name = " + name);
/**
* 文件的名称
*/
System.out.println("filename = " + filename);
/**
* 获取文件流
*/
System.out.println("inputStream = " + inputStream); String uploadDir = context.getRealPath("/uploadfiles");
System.out.println(uploadDir); filename = System.currentTimeMillis() + "_" + filename;
File destFile = new File(uploadDir + "/" + filename);
faceimage.transferTo(destFile); return null;
}
}

二、异步上传文件

  • 导入common-fileupload这个jar包、配置     springmvc-servlet.xml 如上

  • index.jsp
    2:ajax来提交。
<form method="post" id="file_form2" action=""
enctype="multipart/form-data">
<input type="text" name="dogid" id="dogid" value="用户名"/>
<input type="text" name="dogname" id="dogname" value="复选框1"/>
<input type="file" name="faceimage" id="faceimage" size="40"/>
<input type="button" value="上传文件" onclick="uploadFile();"/>
</form>
  • controller
package com.shyroke.controller;

import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map; import javax.servlet.ServletContext; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile; import com.shyroke.bean.DogBean; @Controller
@RequestMapping(value = "/uploadController")
public class UploadController { @Autowired
private ServletContext context; @RequestMapping(value = "/uploadAjax")
@ResponseBody
public Map<String, Object> uploadAjax(DogBean dogBean,
@RequestParam(name = "faceimage", required = false) MultipartFile faceimage) throws Exception { System.out.println(dogBean); System.out.println(faceimage); String dogid = dogBean.getDogid();
String dogname = dogBean.getDogname(); String name = faceimage.getName();
String filename = faceimage.getOriginalFilename();
InputStream inputStream = faceimage.getInputStream();
/**
* 输出控件的名称,faceimage
*/
System.out.println("name = " + name);
/**
* 文件的名称
*/
System.out.println("filename = " + filename);
/**
* 获取文件流
*/
System.out.println("inputStream = " + inputStream); String uploadDir = context.getRealPath("/uploadfiles");
System.out.println(uploadDir);
Map<String, Object> jsonMap = new HashMap<String, Object>();
try {
filename = System.currentTimeMillis() + "_" + filename;
File destFile = new File(uploadDir + "/" + filename);
faceimage.transferTo(destFile);
jsonMap.put("flag", true);
} catch (Exception e) {
jsonMap.put("flag", false);
jsonMap.put("errorMsg", "错误");
}
return jsonMap;
}
}

(十)springmvc之文件的处理的更多相关文章

  1. springmvc图片文件上传接口

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

  2. SpringMVC学习--文件上传

    简介 文件上传是web开发中常见的需求之一,springMVC将文件上传进行了集成,可以方便快捷的进行开发. springmvc中对多部件类型解析 在 页面form中提交enctype="m ...

  3. Spring +SpringMVC 实现文件上传功能。。。

    要实现Spring +SpringMVC  实现文件上传功能. 第一步:下载 第二步: 新建一个web项目导入Spring 和SpringMVC的jar包(在MyEclipse里有自动生成spring ...

  4. JAVA之旅(二十九)——文件递归,File结束练习,Properties,Properties存取配置文件,load,Properties的小练习

    JAVA之旅(二十九)--文件递归,File结束练习,Properties,Properties存取配置文件,load,Properties的小练习 我们继续学习File 一.文件递归 我们可以来实现 ...

  5. JAVA之旅(二十五)——文件复制,字符流的缓冲区,BufferedWriter,BufferedReader,通过缓冲区复制文件,readLine工作原理,自定义readLine

    JAVA之旅(二十五)--文件复制,字符流的缓冲区,BufferedWriter,BufferedReader,通过缓冲区复制文件,readLine工作原理,自定义readLine 我们继续IO上个篇 ...

  6. Django学习之十: staticfile 静态文件

    目录 Django学习之十: staticfile 静态文件 理解阐述 静态文件 Django对静态文件的处理 其它方面 总结 Django学习之十: staticfile 静态文件 理解阐述     ...

  7. 使用springmvc进行文件的上传和下载

    文件的上传 SpringMVC支持文件上传组件,commons-fileupload,commons-fileupload依赖commons-io组件 配置步骤说明 第一步:导入包 commons-f ...

  8. SpringMVC之文件上传异常处理

    一般情况下,对上传的文件会进行大小的限制.如果超过指定大小时会抛出异常,一般会对异常进行捕获并友好的显示出来.以下用SpringMVC之文件上传进行完善. 首先配置CommonsMultipartRe ...

  9. C语言第十二讲,文件操作.

    C语言第十二讲,文件操作. 一丶文件操作概述 在操作系统中,我们的文档都称为文件.操作系统也为我们提供了接口进行操作.不同语言都是使用的相同的接口,只不过封装的上层接口不一样 操作文件的步骤 打开文件 ...

  10. springmvc实现文件上传

    springmvc实现文件上传 多数文件上传都是通过表单形式提交给后台服务器的,因此,要实现文件上传功能,就需要提供一个文件上传的表单,而该表单就要满足以下3个条件 (1)form表彰的method属 ...

随机推荐

  1. TynSerial文件序列(还原)

    TynSerial文件序列(还原) 1)下载文件 procedure TForm1.DownFile(filename: string); // 下载文件 var url: SockString; i ...

  2. arcgis python 布局中所有元素信息报告

    # Author: ESRI # Date: July 5, 2010 # Version: ArcGIS 10.0 # Purpose: This script generates a report ...

  3. Linux中touch命令使用(创建文件)

    touch命令有两个功能: 1.用于把已存在文件的时间标签更新为系统当前的时间(默认方式),它们的数据将原封不动地保留下来: 2.用来创建新的空文件. 语法 touch(选项)(参数) 选项 -a:或 ...

  4. sql查询表的所有字段和表字段对应的类型

    1.查询表的所有字段 select syscolumns.name from syscolumns where id=object_id('写上要查询的表名') 2.查询表的所有字段+表字段对应的类型 ...

  5. usb 设备 复位

    How to Reset USB Device in Linux by ROMAN10 on MAY 4, 2011 · 9 COMMENTS   USB devices are anywhere n ...

  6. http-proxy-middleware及express实现反向代理

    $ npm install --save-dev http-proxy-middleware npm install express // 引用依赖 var express = require('ex ...

  7. SQL注入自学[第一学:一个简单的注入环境的编写]

    /* 转载请注明出处 ID:珍惜少年时 */ CODE区域: /*注:现在mysql_connect的这种连接方式已经被放弃了,也就是说不用了,老夫也是新手上路故,下载了一个wampserver2.2 ...

  8. BitmapShader填充图形

    package com.loaderman.customviewdemo; import android.content.Context; import android.graphics.*; imp ...

  9. Delphi分割字符串函数Split源码

    function TStringHelper.Split(const Separator: array of string; Count: Integer; Options: TStringSplit ...

  10. redis未设置idle超时时间导致连接过多

    今天ELK收集日志的时候,发现收集失败,查找各方面原因,最后在redis日志里面发现报错:[2489] 02 Jun 10:43:42 # Error allocating resoures for ...