首先,我们可以使用URLEncoder来对文件名进行编码: importjava.io.*;importjavax.servlet.*;importjavax.servlet.http.*;importjava.net.URLEncoder;publicclassFileDownloadServletextendsHttpServlet{protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{StringfileName="exam...
response.setHeader("content-type","text/html;charset=utf-8"); response.getWriter().write(""+name+",欢迎您!"); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("Post..."); } } 1. 2. 3. 4...
* content - dispostion:attachment;filename=xxx * 步骤: 1. 定义页面,编辑超链接的href属性,指向servlet,传递资源名称filename 下载页面 2. 定义servlet 1. 获取文件名称 2. 使用字节输入流加载文件进内存 3. 指定response的响应头:content - dispostion:attachment;filename=xxx 4. 将数据写出到response的输出...
}else{//其它浏览器filenameEncoder= URLEncoder.encode(filename,"utf-8"); }//告知浏览器文件的类型(响应体)response.setContentType(getServletContext().getMimeType(filename));//告知浏览器以附件的方式提供下载功能 而不是解析response.setHeader("Content-Disposition","attachment;filename="+filenameEncod...
File file = new File("D:\\file\\1.jpg"); byte[] bytes = Files.toByteArray(file); out.write(bytes); out.close(); } 2. 前台代码 3.整理 response在头部设置 "Content-Disposition" 为 "attachment; fileName= xxx" , xxx为文件名称,带后缀的文件名称,设置之后,文件下载的名称就直接是设置的...
java 使用response导出文件 简介:java 使用response导出文件 try { // path是指欲下载的文件的路径。Filefile=newFile(pathName);// 以流的形式下载文件。InputStream fis =newBufferedInputStream(newFileInputStream(pathName));byte[] buffer =newbyte[fis.available()];...
DownloadController { @RequestMapping("toIndex") public ModelAndView toIndex(HttpServletRequest request, HttpServletResponse response){ //想数据库请求查询得到原始文件名展示 ModelAndView mv = new ModelAndView("index"); mv.addObject("name","火影"); return mv; } @ResponseBody @Requ...
String path = getServletContext().getRealPath("download/"+filename); //获取文件的输入流 InputStream in = new FileInputStream(path); //获得输出流 ServletOutputStream out = response.getOutputStream(); //文件拷贝 int len = 0; byte[] buffer = new byte[1024]; ...
1. HttpServletResponse Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象、和代表响应的response对象。 1.1 响应正文(主体) getWrite(); 字符输出流 getOutputStream(); 字节输出流 两者不能同时使用 1.2 响应编码 ...
java文件下载设置中文名称的实例(response.addHeader) 实例如下: protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String browser = ""; String fileName = "测试.txt"; try { browser = request.getHeader("User-Agent"); ...