博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springMVC文件上传优化
阅读量:5861 次
发布时间:2019-06-19

本文共 4365 字,大约阅读时间需要 14 分钟。

1. 新建web project

2. 填 jar,注意新加入两个上传文件的jar, commons-io, commons-fileupload

3. 改写web.xml 

index.jsp
springMVC
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath*:config/spring-servlet.xml
1
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
characterEncodingFilter
/*
springMVC
/

4. 新增config包, 新建spring-servlet.xml

   新增了上传文件需要的bean 

5. 写controller方法, uploadController.java

package com.tgb.web.controller;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.util.Date;import java.util.Iterator;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;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("/file")public class UploadController {	@RequestMapping("/upload")	public String upload(@RequestParam("file") CommonsMultipartFile file,HttpServletRequest request) throws IOException{		System.out.println("filename--->"+file.getOriginalFilename());		if(!file.isEmpty()){			try {				FileOutputStream os = new FileOutputStream("D:/"+new Date().getTime()+file.getOriginalFilename());				InputStream in =  file.getInputStream();				int b = 0;				while((b=in.read())!=-1){					os.write(b);				}				os.flush();				os.close();				in.close();			} catch (FileNotFoundException e) {				// TODO Auto-generated catch block				e.printStackTrace();			}		}		return "/success"; 	}		@RequestMapping("/upload2")	public String upload2(HttpServletRequest request, HttpServletResponse response) throws IllegalStateException, IOException{		CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());		if(multipartResolver.isMultipart(request)){			MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;			Iterator
iter = multiRequest.getFileNames(); if(iter.hasNext()){ MultipartFile file = multiRequest.getFile((String)iter.next()); if(file!=null){ String fileName="demoUpload"+file.getOriginalFilename(); String path = "D:/" + fileName; File localFile = new File(path); file.transferTo(localFile); } } } return "/success"; } @RequestMapping("/toUpload") public String toUpload(){ return "/upload"; }}

  

6. 写jsp文件, upload.jsp 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>        My JSP 'upload.jsp' starting page       	  
添加用户
选择文件:

还有succuss.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>              	  上传文件成功  

  

7. 地址栏验证

http://localhost:8080/springMVCUpload/file/toUpload

  

    

  

 

转载于:https://www.cnblogs.com/wujixing/p/5604058.html

你可能感兴趣的文章
MySQL函数怎么加锁_MYSQL 函数调用导致自动生成共享锁问题
查看>>
Dynamic Performance Tables not accessible Automatic Statistics Disabled for this session
查看>>
MR1和MR2的工作原理
查看>>
Eclipse中修改代码格式
查看>>
GRUB Legacy
查看>>
关于 error: LINK1123: failure during conversion to COFF: file invalid or corrupt 错误的解决方案...
查看>>
我的友情链接
查看>>
hexo博客解决不蒜子统计无法显示问题
查看>>
python实现链表
查看>>
java查找string1和string2是不是含有相同的字母种类和数量(string1是否是string2的重新组合)...
查看>>
Android TabActivity使用方法
查看>>
java ShutdownHook介绍与使用
查看>>
Eclipse的 window-->preferences里面没有Android选项
查看>>
《麦田里的守望者》--[美]杰罗姆·大卫·塞林格
查看>>
[置顶] 深入探析Java线程锁机制
查看>>
遇到的那些坑
查看>>
央行下属的上海资信网络金融征信系统(NFCS)签约机构数量突破800家
查看>>
[转] Lazy evaluation
查看>>
常用查找算法总结
查看>>
grep 零宽断言
查看>>