抽空写了一个用于下载文件的控制器类,只需要把文件的路径通过参数name传递到后台即可完成文件下载到本地,非常方便~
控制器类代码
package cn.edu.sgu.www.download.controller;import cn.edu.sgu.www.download.entity.RequestURI;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;/*** @author heyunlin* @version 1.0*/
@RestController
@RequestMapping(produces = "application/json;charset=utf-8")
public class DownLoadController {/*** 下载单张图片* 接口请求路径:localhost:8083/download?name=文件路径*/@RequestMapping(value = "/download", method = RequestMethod.GET)public void download(HttpServletRequest request, HttpServletResponse response) {StringBuilder sb = new StringBuilder();ServletOutputStream outputStream = null;InputStream inputStream = null;try {String name = request.getParameter("name");RequestURI requestURI = parse(name);String fileName = requestURI.getName();sb.append(requestURI.getPath()).append(fileName);URL url = new URL(requestURI.getProtocol(), requestURI.getHost(), sb.toString());// 设置响应头response.setContentType("application/octet-stream");response.setHeader("Content-Disposition", "attachment;filename=" + fileName);outputStream = response.getOutputStream();inputStream = url.openConnection().getInputStream();byte[] buffer = new byte[1024];int len;while ((len = inputStream.read(buffer)) != -1) {outputStream.write(buffer, 0, len);}outputStream.flush();} catch (IOException e) {e.printStackTrace();} finally {try {if(inputStream != null) {inputStream.close();}if(outputStream != null) {outputStream.close();}} catch (IOException e) {e.printStackTrace();}}}/*** 解析URL* @param name 文件url* @return RequestURI*/private RequestURI parse(String name) {int index = name.indexOf("//"); // 第一个//的位置int firstIndex; // 第1个/的位置String host; // 主机String path = ""; // 文件路径String protocol; // 协议// 没有指定协议,默认为httpif (index < 0) {firstIndex = name.indexOf("/"); // 第1个/的位置host = name.substring(0, firstIndex);protocol = "http";} else {firstIndex = name.indexOf("/", index + 2); // 第1个/的位置host = name.substring(index + 2, firstIndex);protocol = name.substring(0, index -1);}// 得到最后一个/的位置int lastIndex = name.lastIndexOf("/");if (firstIndex != lastIndex) {path = name.substring(firstIndex, lastIndex + 1);}String filename = name.substring(lastIndex + 1);System.out.println("协议 ==> " + protocol);System.out.println("服务器 ==> " + host);System.out.println("文件路径 ==> " + path);System.out.println("文件名 ==> " + filename);RequestURI requestURI = new RequestURI();requestURI.setProtocol(protocol);requestURI.setHost(host);requestURI.setPath(path);requestURI.setName(filename);return requestURI;}}
实体类代码
package cn.edu.sgu.www.download.entity;import lombok.Data;/*** @author heyunlin* @version 1.0*/
@Data
public class RequestURI {/*** 协议*/private String protocol;/*** 主机域名*/private String host;/*** 文件路径*/private String path;/*** 文件名*/private String name;
}