package com.hui10.app.controller.merchant;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.OutputStream;
import java.net.URL;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.hui10.app.common.web.BaseController;
@Controller
public class TestController extends BaseController {
/**
* 测试文件下载
* @throws Exception
* @throws FileNotFoundException
*/
@RequestMapping(value = "/testhh/download", produces = { "application/json;charset=UTF-8" }, method = RequestMethod.GET)
@ResponseBody
public ModelAndView downloadFile(HttpServletResponse response) throws Exception {
downLoad("D://timg.jpg",response,true);
return null;
}
public void downLoad(String filePath, HttpServletResponse response, boolean isOnLine) throws Exception {
File f = new File(filePath);
if (!f.exists()) {
response.sendError(404, "File not found!");
return;
}
BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
byte[] buf = new byte[1024];
int len = 0;
response.reset(); // 非常重要
if (isOnLine) { // 在线打开方式
URL u = new URL("file:///" + filePath);
response.setContentType(u.openConnection().getContentType());
response.setHeader("Content-Disposition", "inline; filename=" + f.getName());
// 文件名应该编码成UTF-8
} else { // 纯下载方式
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment; filename=" + f.getName());
}
OutputStream out = response.getOutputStream();
while ((len = br.read(buf)) > 0)
out.write(buf, 0, len);
br.close();
out.close();
}
参考地址:https://blog.csdn.net/pangqiandou/article/details/53234586
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.OutputStream;
import java.net.URL;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.hui10.app.common.web.BaseController;
@Controller
public class TestController extends BaseController {
/**
* 测试文件下载
* @throws Exception
* @throws FileNotFoundException
*/
@RequestMapping(value = "/testhh/download", produces = { "application/json;charset=UTF-8" }, method = RequestMethod.GET)
@ResponseBody
public ModelAndView downloadFile(HttpServletResponse response) throws Exception {
downLoad("D://timg.jpg",response,true);
return null;
}
public void downLoad(String filePath, HttpServletResponse response, boolean isOnLine) throws Exception {
File f = new File(filePath);
if (!f.exists()) {
response.sendError(404, "File not found!");
return;
}
BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
byte[] buf = new byte[1024];
int len = 0;
response.reset(); // 非常重要
if (isOnLine) { // 在线打开方式
URL u = new URL("file:///" + filePath);
response.setContentType(u.openConnection().getContentType());
response.setHeader("Content-Disposition", "inline; filename=" + f.getName());
// 文件名应该编码成UTF-8
} else { // 纯下载方式
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment; filename=" + f.getName());
}
OutputStream out = response.getOutputStream();
while ((len = br.read(buf)) > 0)
out.write(buf, 0, len);
br.close();
out.close();
}
}
浏览器输入测试地址,结果如下:
![](https://i-blog.csdnimg.cn/blog_migrate/ed1a8ef4145b72954af59ab776f67a52.png)
参考地址:https://blog.csdn.net/pangqiandou/article/details/53234586