springmvc(2)上传和下载以及静态资源处理

文件上传
导入jar包

在这里插入图片描述

jsp页面

<body>
	<a href="down">下载地址1</a>
	<a href="down1">下载地址2</a>
	<form action="upload" method="post" enctype="multipart/form-data"> 
		<table>
			<tr>
				<th>用户</th>
				<td>
					<input type="text" name="name" value="" />
				</td>
			</tr>
			<tr>
				<th>地址</th>
				<td>
					<input type="text" name="address" value="" />
				</td>
			</tr>
			<tr>
				<td>
						<input type="file" name="file" value="" />
				</td>
			</tr>
			<tr>
				<td><input type="submit" value="注册"></td>
			</tr>
		</table>
	</form>
</body>

xml配置文件

	<!-- 开启扫描 -->
 	<context:component-scan base-package="com.liy.controller"></context:component-scan>
 	
 	<!-- 开启mvc注解 -->
 	<mvc:annotation-driven></mvc:annotation-driven>
 	
 	<bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
		id="multipartResolver" >
		<!-- 设置上传文件信息参数 -->
		<!-- 设置文件上传的最大尺寸 -->
		<property name="maxUploadSize">
			<value>5242880</value>
		</property>
	</bean>
 
 	<!-- 配置文件的允许访问静态资源 -->
 	<mvc:resources location="/img/" mapping="/img/**"></mvc:resources>
 
 

controller层代码

@Controller
public class UploadController {
	
	@RequestMapping("/upload")
	@ResponseBody
	public void upload(String name,String address,MultipartFile file) throws IllegalStateException, IOException{
		
		System.out.println(name+"--"+address+"--"+file.getOriginalFilename());
		
		file.transferTo(new File("d:/img/imgs/"+file.getOriginalFilename()));
		
	}

下载的controller代码(两种方式)

@RequestMapping("down")
	@ResponseBody
	public void down(HttpServletRequest request , HttpServletResponse response) throws IOException{
		
		File file = new File("d:/img/2.png");
		System.out.println(file.getName());
		//设置响应头和客户端保存文件名
	    response.setCharacterEncoding("utf-8");
	    response.setContentType("multipart/form-data");
	    response.setHeader("Content-Disposition", "fileName=" + file.getName());
 
	   //打开本地文件流
       
		InputStream in = new FileInputStream(file);
		
		OutputStream out = response.getOutputStream();
		
		byte [] b = new byte[1024]; 
		
		int len;
		 
		while ((len = in.read(b)) > 0 ) {
			 out.write(b , 0 ,len);
			
		}

		out.close();
		in.close(); 
	}
	
	@RequestMapping("/down1")
	public ResponseEntity<byte[]> download(HttpServletRequest request) throws IOException {
	    // 需要下载的文件
		File file = new File("d:/img/2.png");
	    byte[] body = null;
	    InputStream is = new FileInputStream(file);
	    body = new byte[is.available()];
	    is.read(body);
	    HttpHeaders headers = new HttpHeaders();
	    headers.add("Content-Disposition", "attchement;filename=" + file.getName());
	    HttpStatus statusCode = HttpStatus.OK;
	    
	    ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(body, headers, statusCode);
	    return entity;
	}
	 
 

 

在SpringMVC中,默认情况下,所有的静态资源都会被拦截(js,css。html,图片、视频、音频),对于静态资源,需要手动配置静态资源过滤。
解决这个问题的方式有两种:

在web.xml中配置default servlet

  <servlet-mapping>
  	<servlet-name>default</servlet-name>
  	<url-pattern>*.js</url-pattern>
  </servlet-mapping>
 <!--  
   <servlet-mapping>
  	<servlet-name>default</servlet-name>
  	<url-pattern>*.jpg</url-pattern>
  </servlet-mapping>
   -->
  
  <servlet-mapping> 
  	<servlet-name>default</servlet-name>
  	<url-pattern>*.png</url-pattern>
  </servlet-mapping>
 

在配置文件中通过标签设置

	<!-- 配置文件的允许访问静态资源 -->
 	<mvc:resources location="/img/" mapping="/img/**"></mvc:resources>

    <mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值