SSM第3天 RequestMapping与文件上传

一、中午乱码问题

<filter>
	<filter-name>characterEncodingFilter</filter-name>
	<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
	<init-param>
		<param-name>encoding</param-name>
		<param-value>UTF-8</param-value>
	</init-param>
	<init-param>
		<param-name>forceEncoding</param-name>
		<param-value>true</param-value>
	</init-param>
</filter>
<filter-mapping>
	<filter-name>characterEncodingFilter</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>

二、静态资源映射

<mvc:annotation-driven/>
<mvc:resources location="/img/" mapping="/img/**"/>   
<mvc:resources location="/js/" mapping="/js/**"/>    
<mvc:resources location="/css/" mapping="/css/**"/>  

location:实际文件目录
mapping:请求链接映射的目录

三、@RequestMapping

3.1 value

value:指定请求的实际地址,指定的地址可以是URI Template 模式;


@RequestMapping(value = "2.do")
public String m2(){
	System.out.println("-----------------1");
	return null;
}


@RequestMapping(value= {"5.do","6.do"})
public String m5(){
	System.out.println("-----------------5");
	return null;
}

@RequestMapping("7.do/{id}")
public String m7(@PathVariable String id){
	System.out.println("-----------------7"+id);
	return null;
}

@RequestMapping("8.do")
public String m8(@RequestParam("id") String id2){
	System.out.println("-----------------8:"+id2);
	return null;
}

3.2 method

method: 指定请求的method类型, GET、POST、PUT、DELETE等;


@RequestMapping(value="3.do",method={RequestMethod.POST})
public String m3(){
	System.out.println("-----------------1");
	return null;
}

3.3 params

params: 指定request中必须包含某些参数值时,才让该方法处理。


@RequestMapping(value="4.do",params={"name","age"})
public String m4(String name){
	System.out.println("-----------------4"+name);
	return null;
}

3.4 consumes、produces、headers

consumes: 指定处理请求的提交内容类型(Content-Type),例如application/json, text/html;

produces: 指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回;

headers: 指定request中必须包含某些指定的header值,才能让该方法处理请求。

四、文件上传问题

4.0 Maven Jar

<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
	<groupId>commons-io</groupId>
	<artifactId>commons-io</artifactId>
	<version>2.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
	<groupId>commons-fileupload</groupId>
	<artifactId>commons-fileupload</artifactId>
	<version>1.3.1</version>
</dependency>

4.1 xxx-servlet.xml

<bean id="multipartResolver" 
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
	<property name="maxUploadSize" value="104857600" />
	<property name="maxInMemorySize" value="4096" />
	<property name="defaultEncoding" value="UTF-8"></property>
</bean>

4.2 JSP页面

tips:
    1.方法必须POST
    2.enctype="multipart/form-data"
    3.input 类型为file

<form action="submit.action" method="post" enctype="multipart/form-data">
	<h1>使用spring mvc提供的类的方法上传文件</h1>
	<input type="file" name="file">
	<input type="submit" value="upload" />
</form>

4.3 Controller

@ResponseBody
@RequestMapping("submit.do")
public String springUpload(@RequestParam("file")MultipartFile file) throws Exception {
	String path = "D:/";
	String fileName = System.currentTimeMillis() +file.getOriginalFilename();
	path += fileName;
	System.out.println(path);
	//保存文件
	file.transferTo(new File(path));
	return path;
}

4.4 数据库管理

最终目的的把数据存放在数据库中;
文件是放在磁盘中的,数据库里存放的是文件的url
当页面展示图片的时候,是从数据库中查询图片的url
然后通过url查询文件,最后显示到页面

五、文件下载(自学)


import java.io.File;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;





@RequestMapping(value = "/download.do")
public ResponseEntity<byte[]> DownloadFile(String filename,HttpServletRequest req)
		throws ServletException, IOException {
	// 接受的是UTF-8
	req.setCharacterEncoding("utf-8");
	// 获取项目根目录
	String path = "D:/img/"+filename;
	// 获取文件名
	File file = null;
	HttpHeaders headers = null;
	try {
		file = new File(path);
		// 请求头
		headers = new HttpHeaders();
		String fileName1 = new String(filename.getBytes("UTF-8"), "iso-8859-1");// 解决文件名乱码
		// 通知浏览器以attachment(下载方式)打开图片
		headers.setContentDispositionFormData("attachment", fileName1);
		// application/octet-stream二进制流数据(最常见的文件下载)。
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
	} catch (Exception e) {
		throw new RuntimeException(e);
	}
	return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.OK);
}
发布了19 篇原创文章 · 获赞 17 · 访问量 6272

猜你喜欢

转载自blog.csdn.net/s3219153/article/details/104510532
今日推荐