springmvc瞭解(2) springmvc的上傳和下載 以及靜態資源的處理

文件上傳

導入jar包css

 

jsp頁面html

<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配置文件web

<!-- 開啓掃描 -->
 	<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層代碼spring

@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,圖片、視頻、音頻),對於靜態資源,須要手動配置靜態資源過濾。
解決這個問題的方式有兩種:mvc

在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>
相關文章
相關標籤/搜索