SpringMVC上传文件FileUpload使用方法详解

网友投稿 303 2023-03-15


SpringMVC上传文件FileUpload使用方法详解

本文实例为大家分享了SpringMVC上传文件FileUpload的具体代码,供大家参考,具体内容如下

我是在已经搭建好的springMVC环境下,maven工程中的pom.xml所需要的jar包(其中spring上传文件的两个主要jar:commons-fileupload.jar和commons-io.jar):

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

4.0.0

com.zhihua

SpringMVC_FileUpload

war

0.0.1-SNAPSHOT

SpringMVC_FileUpload Maven Webapp

http://maven.apache.org

3.2.8.RELEASE

4.10

junit

junit

3.8.1

test

commons-fileupload

commons-fileupload

1.3.1

org.springframework

spring-core

${spring.version}

org.springframework

spring-webmvc

${spring.version}

org.springframework

spring-context

${spring.version}

org.springframework

spring-context-support

${spring.version}

org.springframework

spring-aop

${spring.version}

org.springframework

spring-aspects

${spring.version}

org.springframework

spring-tx

${spring.version}

org.springframework

spring-jdbc

${spring.version}

org.springframework

spring-web

${spring.version}

org.springframework

spring-test

${spring.version}

test

jstl

jstl

1.2

javax.servlet

javax.servlet-api

3.0.1

provided

jsp-api

2.1

provided

SpringMVC_FileUpload

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

4.0.0

com.zhihua

SpringMVC_FileUpload

war

0.0.1-SNAPSHOT

SpringMVC_FileUpload Maven Webapp

http://maven.apache.org

3.2.8.RELEASE

4.10

junit

junit

3.8.1

test

commons-fileupload

commons-fileupload

1.3.1

org.springframework

spring-core

${spring.version}

org.springframework

spring-webmvc

${spring.version}

org.springframework

spring-context

${spring.version}

org.springframework

spring-context-support

${spring.version}

org.springframework

spring-aop

${spring.version}

org.springframework

spring-aspects

${spring.version}

org.springframework

spring-tx

${spring.version}

org.springframework

spring-jdbc

${spring.version}

org.springframework

spring-web

${spring.version}

org.springframework

spring-test

${spring.version}

test

jstl

jstl

1.2

javax.servlet

javax.servlet-api

3.0.1

provided

jsp-api

2.1

provided

SpringMVC_FileUpload

在spring-mvc.xml中添加代码:

class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

前端页面(fileUpload.jsp)和上传成功返回页面(success.jsp):

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>




<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

controller 的代码:

package com.zhihua.controller;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import EfZszNjava.util.Date;

import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;

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 org.springframework.web.multipart.MultipartHttpServletRequest;

import org.springframework.web.multipart.commons.CommonsMultipartFile;

import org.springframework.web.multipart.commons.CommonsMultipartResolver;

@Controller

@RequestMapping("/fileUpload")

public class FileUploadController {

/**

* 通过流的方式上传文件

* @RequestParam("file") 将name=file控件得到的文件封装成CommonsMultipartFile 对象

* <请替换成功能描述>

* <请替换成详细描述>

* @param file

* @return

* @author caizh

* @since [1.0.0]

* @version [1.0.0,2017年2月5日]

*/

@RequestMapping("/upload_01")

public String fileUpload_1(@RequestParam("file_01")CommonsMultipartFile file)throws IOException {

//用来检测程序运行时间

long startTime = System.currentTimeMillis();

System.out.println("fileName:"+file.getOriginalFilename());

try{

//获取输出流

OutputStream os = new FileOutputStream("E:/"+new Date().getTime()+file.getOriginalFilename());

//获取输入流CommonsMultipartFile中可以直接得到文件的流

InputStream is = file.getInputStream();

int temp;

//一个一个字节的读取并写入

while((temp=is.read())!=(-1)){

os.write(temp);

}

os.flush();

os.close();

is.close();

}catch(Exception e){

e.printStackTrace();

}

long endTime = System.currentTimeMillis();

System.out.println("方法一的运行时间:"+String.valueOf(endTime-startTime)+"ms");

return "view/success";

}

/**

* 采用file.Transto 来保存上传的文件

* <请替换成功能描述>

* <请替换成详细描述>

* @param file

* @return

* @throws IOException

* @author caizh

* @since [1.0.0]

* @version [1.0.0,2017年2月5日]

*/

@RequestMapping("/upload_02")

public String fileUpload_2(@RequestParam("file_02") CommonsMultipartFile file)throws IOException {

long startTime = System.currentTimeMillis();

System.out.println("fileName:"+file.getOriginalFilename());

String path = "E:/"+new Date().getTime()+file.getOriginalFilename();

File newFile = new File(path);

//通过CommonsMultipartFile 的方法直接写文件

file.transferTo(newFile);

long endTime = System.currentTimeMillis();

System.out.println("方法二的运行时间:"+String.valueOf(endTime-startTime)+"ms");

return "view/success";

}

/**

* 使用MultipartFile上传文件

* <请替换成功能描述>

* <请替换成详细描述>

* @param file

* @return

* @author caizh

* @since [1.0.0]

* @version [1.0.0,2017年2月5日]

*/

@RequestMapping("/upload_03")

public String fileUpload_3(@RequestParam(value="file_03",required=false)MultipartFile file,

HttpServletRequest request){

String path = request.getSession().getServletContext().getRealPath("upload");

System.out.println(path);

String fileName = file.getOriginalFilename();

File targetFile = new File(path,fileName);

if(!targetFile.exists()){

targetFile.mkdirs();

}

//保存

try {

file.transferTo(targetFile);

}catch(Exception e){

e.printStackTrace();

}

return "view/success";

}

/**

* 采用spring提供的上传文件的方法

* <请替换成功能描述>

* <请替换成详细描述>

* @param request

* @return

* @throws IllegalStateException

* @throws IOException

* @author caizh

* @since [1.0.0]

* @version [1.0.0,2017年2月5日]

*/

@RequestMapping("/upload_spring")

public String springUpload(HttpServletRequest request)throws IllegalStateException, IOException{

long startTime = System.currentTimeMillis();

//将当前上下文初始化给 CommonsMutipartResolver (多部分解析器)

CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());

//检查form中是否有enctype="multipart/form-data"

if(multipartResolver.isMultipart(request))

{

//将request变成多request

MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;

//获取multiRequest中所有的文件名

Iterator iter = multiRequest.getFileNames();

while(iter.hasNext()){

//一次遍历所有的文件

MultipartFile file = multiRequest.getFile(iter.next().toString());

if(file!=null){

String path = "E:/springUpload"+file.getOriginalFilename();

//上传

file.transferTo(new File(path));

}

}

}

long endTime = System.currentTimeMillis();

System.out.println("方法三的运行时间:"+String.valueOf(endTime-startTime)+"ms");

return "view/success";

}

}

好了,全部代码都给出了,博主是测试成功了,感兴趣的朋友可以去比较不同上传方式的效率!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:接口自动化测试管理平台(接口自动化测试方案详解)
下一篇:网关API怎么写(api网关的作用是什么)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~