使用springMVC框架实现文件下载

package com.example.study.springmvc.controller;


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


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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
public class DownLoadController {


/**
* @Description 下载文件
* @param request  
* HttpServletRequest对象
* @param srcFileName
* 需要下载的文件名,@RequestParam("fileName")表示请求参数名为fileName
* @return
* @throws IOException
*/
@RequestMapping("/download")
@ResponseBody
public ResponseEntity<byte[]> download(HttpServletRequest request, 
@RequestParam("fileName") String srcFileName) throws IOException {

// 下载的目标文件路径
String destPath = request.getServletContext().getRealPath("WEB-INF/upload/");
// 下载的目标文件
File destFile = new File(destPath + srcFileName);
// 处理下载文件名中文乱码问题
String downFileName = new String(srcFileName.getBytes("UTF-8"), "iso-8859-1");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentDispositionFormData("attachment", downFileName);
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(destFile), 
headers, HttpStatus.CREATED);

}

}


jsp页面:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>download页面</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/download?fileName=中文.txt ">下载1.txt</a><br/><br/>
<a href="${pageContext.request.contextPath}/download?fileName=2.txt ">下载2.txt</a><br/><br/>
<a href="${pageContext.request.contextPath}/download?fileName=1.jpg ">下载1.jpg</a><br/><br/>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringMVC实现文件上传需要以下步骤: 1. 添加MultipartResolver配置 在SpringMVC的配置文件中添加以下配置: ``` <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="10485760"/> <!-- 10MB --> </bean> ``` 这个配置将会让SpringMVC知道如何处理文件上传请求,并设置最大上传文件大小为10MB。 2. 创建文件上传表单 在JSP或HTML文件中创建一个文件上传表单,可以使用`<form>`元素,像这样: ``` <form action="<c:url value="/upload"/>" method="post" enctype="multipart/form-data"> <input type="file" name="file"/> <input type="submit" value="Upload"/> </form> ``` 注意:`enctype`属性必须设置为`multipart/form-data`。 3. 创建Controller方法处理文件上传请求 创建一个Controller方法处理上传请求,像这样: ``` @RequestMapping(value = "/upload", method = RequestMethod.POST) public String handleFileUpload(@RequestParam("file") MultipartFile file, ModelMap model) { if (!file.isEmpty()) { try { byte[] bytes = file.getBytes(); // Do something with the bytes model.addAttribute("message", "File '" + file.getOriginalFilename() + "' uploaded successfully"); } catch (Exception e) { model.addAttribute("message", "Failed to upload file '" + file.getOriginalFilename() + "'"); } } else { model.addAttribute("message", "File '" + file.getOriginalFilename() + "' is empty"); } return "uploadResult"; } ``` 4. 处理上传结果 当文件上传完成后,我们可以把上传结果渲染到视图中,像这样: ``` <html> <head> <title>Upload Result</title> </head> <body> <h1>${message}</h1> </body> </html> ``` 这样就完成了在SpringMVC实现文件上传的过程。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值