Spring MVC 实现文件下载 【Spring MVC 学习笔记 五】

Spring MVC 实现文件上传地址:

https://blog.csdn.net/Kedongyu_/article/details/81454619

文件下载的模式

      1.直接下载模式:把上传的文件直接放在Web的目录,使用HTML标签显示或下载。

 

     <img src=”upload/文件名” />

     <a href=”upload/文件名”>下载</a>

      2.间接下载模式: 文件保存在数据库中,或保存一个非web目录下。只能通过编程取得下载文件。

 

HTTP响应内容

      1.状态码(Status Code)

表达服务器处理请求的状态,是否成功等信息。200 OK, 404,400, 405 ,415, 500

      2.响应头(response header):

包含响应体的类型(ContentType), 字符编码集,响应体长度等信息。

      3.响应体(Response Body):

文本格式: text/html, application/json,  application/xml

二进制格式:image/jpeg, image/gif, audio/mp3, video/mp4

 

原始的Servlet发送HTTP响应编程

      1.文本类型响应:

(1)取得响应对象: HttpServletResponse

(2)设置响应类型:setContentType

(3)设置字符编码集:

(4)取得连接客户端的文本输出流: Writer out=

(5)写数据到输出流。

(6)清空缓存

(7)关闭输出流。

 

      2.二进制类型响应:

(1)取得响应对象: HttpServletResponse

(2)设置响应类型:setContentType

(3)取得连接客户端的二进制输出流: OutputStream out=

(4)写数据到输出流。

(5)清空缓存,

(6)关闭输出流

 

Spring MVC控制器的方法的参数类型

      1.Spring MVC的Model对象:Model:保存给View传递的数据。

      2.业务的Model对象:接收请求的数据。

      3.单独的参数: int page: 接收数据。

      4.地址栏变量: url{page} @PathVariable  int page

 

Spring MVC控制方法的返回类型

      1.String : 表达View的逻辑名, 需要View的解析器进行解析。

      2.void: 无view返回,直接处理响应。

      3.@ResponseBody Java数据类型: 控制器直接发送响应数据。

               默认使用JSON解析器。

               可以返回任何Java数据类型。

      4.HttpEntity<B>,

      5.ResponseEntity<B>: 由控制方法直接返回HTTP响应,

                    B泛型指定响应体的数据类型。

Spring MVC 模式 图片显示:

      编程案例:显示保存在数据库中员工的照片。

      数据库使用Byte[]存放图片,并编写下列代码读取图片,并直接输出:

	@ResponseBody
	@RequestMapping("/togetPhotobyno")
	public void showEmployeePhoto(@RequestParam(required=false) int no,HttpServletResponse response) throws Exception{
		EmployeeModel employee=employeeService.findEmployeeByNo(no);
		if(employee!=null&&employee.getPhotoFileName()!=null) {
			response.setContentType(employee.getPhotoContentType());
			OutputStream out=response.getOutputStream();
                        //其中getPhoto()返回类型是 byte[] 类型
			out.write(employee.getPhoto());
			out.flush();
			out.close();
		}
	}

      其实就是将图片以二进制输出即可实现图片显示,前端只需要通过 togetPhotobyno.mvc 即可显示图片。

      也就是:

     <img src="togetPhotobyno.mvc?no=${employeeNo}" />

      效果:访问http://localhost:8080/employee/togetPhotobyno.mvc?no=262

Spring MVC模式编写文件下载

使用ResponseEntity<B>返回类型,可以实现Spring MVC模式的文件下载。

优点是不依赖Web Servlet API对象,推荐使用此模式。

 基本编程步骤:

  1. 取得响应的类型,包括主类型,子类型。
  2. 创建响应头对象:HttpHeaders
  3. 设置响应头信息:
  4. 创建ResponseEntity<B>对象,设置响应体数据,响应头,状态码。
  5. 返回ResponseEntity<B>对象即可。

 

编程案例:下载保存在数据库中员工的照片。

	@RequestMapping(value="/togetPhotobyno01")
	public ResponseEntity<byte[]> showEmployeePhoto01(@RequestParam(required=false) int no) throws Exception{
		EmployeeModel emp=employeeService.findEmployeeByNo(no);
		if(emp!=null&&emp.getPhotoFileName()!=null) {
			byte[] photo=emp.getPhoto();
			String fileName=emp.getPhotoFileName();
			String contentType=emp.getPhotoContentType();
			String mainType=contentType.substring(0, contentType.indexOf("/"));
			String subType=contentType.substring(contentType.indexOf("/")+1);
			String dfileName=new String (fileName.getBytes("UTF-8"),"iso8859-1");
			HttpHeaders headers=new HttpHeaders();
			headers.setContentType(new MediaType(mainType,subType));
			headers.setContentDispositionFormData("attachment", dfileName);
			return new ResponseEntity<byte[]> (photo,headers,HttpStatus.CREATED);
				
		}
		return null;
	}

      

      访问该网址即可自动下载文件。  

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值