SpringMVC文件上传

SpringMVC文件上传

SpringMVC会根据请求方法签名不同,将请求消息中的信息以一定的方式转换并绑定到其你去方法的参数中。

 

今天我们来说说文件上传

 

文件上传

文件上传是Web项目中最基础的功能了。首先我们来看看文件上传在Spring里是怎么运行的。

 

首先将表单的method设置为POST,并将enctype设置为multipart/form-data,浏览器就会采用二进制的防治来处理表单上的数据,而对于文件上传的处理则设计在服务器端解析原始的HTTP响应。

 

需要的jar包

普通的springmvc的jar包除外,还需要


笔者在写测试项目的时候有出现servlet的jar缺失,这个也是需要加进去的。

 

首先我们先来写jsp代码:

<%@ page language="java" import="java.util.*" contentType="text/html; charset=GB2312" %> 
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <title>文件上传</title>
  <body>
    <h2>注册界面</h2>
    <br>
    <form action="upload" enctype="mulipart/form-data" method="post"></form>
    <table>
    	<tr>
    		<td><label>文件描述:</label></td>
    		<td><input type="text"  name="descirption"></td>
  		</tr>
  		<tr>
  			<td><label>密码:</label></td>
  			<td><input type="password" id="password" name="password"></td>
  		</tr>
  		<tr>
  			<td><input type="submit" value="上传"></td>
  		</tr>
    </table>
  </body>
</html>


控制器代码:

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

public class FileUploadController {
	@RequestMapping(value="/upload",method=RequestMethod.POST)
    public String uploadPosdetailFile(@RequestParam("description") String descrtption, 
                                  @RequestParam("file") MultipartFile file,HttpServletRequest request,HttpServletResponse response) throws Exception{
		System.out.println("description");
		//文件不能为空
		if(!file.isEmpty()){
			//上传文件路径
			String path = request.getServletContext().getRealPath("/images");
			//上传文件名
			String filename = file.getOriginalFilename();
			File filepath = new File(path,filename);
			//判断路径是否存在,如果不存在就创建一个
			if(!filepath.getParentFile().exists()){
				filepath.getParentFile().mkdirs();
			}
			//将上传文件保存到一个目标文件中
			file.transferTo(new File(path+File.separator+filename));
			return "success";
		}
		return "error";
	}
}


Springmvc配置文件代码需要加上:

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">   
            <!-- 上传文件大小上线,单位为字节 -->
	          <property name="maxUploadSize"><value>100000</value></property> 
	          <!-- 请求的编码格式,必须和JSP的怕个Encoding属性一致,以便正确读取表单的内容 -->
	          <property name="defaultEncoding"><value>UTF-8</value></property> 
	   		  </bean> 


Web.xml配置文件就是最基本的,这里不再多加叙述。


这里既然说到文件上传,我就顺便提一提文件下载,SpringMVC提供了一个RespouseEntity类型,使用它可以嫩方便地定义返回的HttpHeaders和HttpStatus。


到这里就经过部署就可以使用浏览器进行测试了,

浏览器输入

http://localhost:8080/FileUploadTest/registerForm

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值