将字符串或文件控制浏览器以下载的形式打开文件
2017-04-26 09:41
337 查看
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
/**
* 将字符串或文件控制浏览器以下载的形式打开文件
* Created by yjli on 2017/4/25.
*/
@Controller
@RequestMapping("/upload")
public class UploadFileController {
@RequestMapping("/upload")
public void test(HttpServletResponse response){
String filename = "a.xml";
String msg = "123456";
response.reset();
response.setContentType("application/x-download");
response.setCharacterEncoding("UTF-8");
try {
//设置content-disposition响应头控制浏览器以下载的形式打开文件
response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes("GB2312"), "iso8859-1"));
// 输出资源内容到相应对象
response.getOutputStream().write(msg.getBytes("utf-8"));
} catch (IOException e) {
e.printStackTrace();
}
}
@RequestMapping("/uploadFile")
public void test1(HttpServletResponse response){
String filename = "a.xml";
String msg = "123456";
response.reset();
response.setContentType("application/x-download");
response.setCharacterEncoding("UTF-8");
try {
// InputStream in = new FileInputStream(new File(filename));
InputStream in = new BufferedInputStream(new ByteArrayInputStream(msg.getBytes("utf-8")));
if (in == null)
return;
//设置content-disposition响应头控制浏览器以下载的形式打开文件
response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes("GB2312"), "iso8859-1"));
// 输出资源内容到相应对象
byte[] b = new byte[1024];
int len;
OutputStream out = response.getOutputStream();
while ((len = in.read(b, 0, 1024)) != -1) {
out.write(b, 0, len);
}
in.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
/**
* 将字符串或文件控制浏览器以下载的形式打开文件
* Created by yjli on 2017/4/25.
*/
@Controller
@RequestMapping("/upload")
public class UploadFileController {
@RequestMapping("/upload")
public void test(HttpServletResponse response){
String filename = "a.xml";
String msg = "123456";
response.reset();
response.setContentType("application/x-download");
response.setCharacterEncoding("UTF-8");
try {
//设置content-disposition响应头控制浏览器以下载的形式打开文件
response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes("GB2312"), "iso8859-1"));
// 输出资源内容到相应对象
response.getOutputStream().write(msg.getBytes("utf-8"));
} catch (IOException e) {
e.printStackTrace();
}
}
@RequestMapping("/uploadFile")
public void test1(HttpServletResponse response){
String filename = "a.xml";
String msg = "123456";
response.reset();
response.setContentType("application/x-download");
response.setCharacterEncoding("UTF-8");
try {
// InputStream in = new FileInputStream(new File(filename));
InputStream in = new BufferedInputStream(new ByteArrayInputStream(msg.getBytes("utf-8")));
if (in == null)
return;
//设置content-disposition响应头控制浏览器以下载的形式打开文件
response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes("GB2312"), "iso8859-1"));
// 输出资源内容到相应对象
byte[] b = new byte[1024];
int len;
OutputStream out = response.getOutputStream();
while ((len = in.read(b, 0, 1024)) != -1) {
out.write(b, 0, len);
}
in.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
相关文章推荐
- 在ASP.NET中下载Text文件,而不是在浏览器中打开它
- 控制Word在浏览器中打开,还是下载
- 利用Content-Disposition控制浏览器下载或直接打开
- Ubuntu下apache2无法解析php文件,浏览器提示下载所要打开的php文件
- 浏览器文件下载和图片显示(流形式)
- 下载WORD文件时,不要打开,以文件形式下载,有没有?
- 屏蔽浏览器打开下载文件方法
- PHP实现点击a标签的href做链接时,直接保存文件(任何类型),而不是通过浏览器直接打开下载的文件
- 怎么解决点击下载Csv文件时在浏览器里面打开,而不是下载
- 点击文件下载而不在浏览器中打开
- java 实现文件下载的时候,避免浏览器自动打开文件,而是出现另存为对话框
- Java 下载文件不在浏览器中直接打开
- Java防止浏览器直接打开下载的文件
- 使用Servlet实现文件下载的时候,避免浏览器自动打开文件
- 直接下载文件,而非在浏览器中打开
- 使用Servlet实现文件下载的时候,避免浏览器自动打开文件
- 默认点击下载pdf等文件而不是在浏览器中打开
- 在文件下载的时候将其保存而不是直接在浏览器上打开!
- 浏览器文件下载和图片显示(流形式)
- 安卓自带浏览器无法打开一些下载的APK文件(tomcat中配置MIME类型)