getResponse的getWriter()方法
连续两次输出流到页面的时候,第二次的流会包括第一次的流,所以可以使用将response.reset或者resetBuffer的方法。
reset():
Clears any data that exists in the buffer as well as the status code and headers. If the response has been committed, this method throws an IllegalStateException.
resetBuffer():
Clears the content of the underlying buffer in the response without clearing headers or status code. If the response has been committed, this method throws an IllegalStateException.
response.reset()的使用有一个条件受限:response的任何打开流关闭之后都不能再reset .
分享到:
相关推荐
- `response.reset()`:重置响应状态,清除之前的响应数据。 - `response.addHeader("Content-Disposition", ...)`:指定以附件形式下载,并设置下载文件名。 - `response.addHeader("Content-Length", ...)`:...
String file_unique = request.getParameter("file_unique"); String date = file_unique.substring(0,7); path = date+"/"+file_... System.out.println(buffer.toString()); toClient.flush(); toClient.close();
response.flushBuffer(); } catch (Exception e) { e.printStackTrace(); } finally { } %} ``` 2. **采用文件流输出的方式**: 这种方法更加直接,它通过读取文件内容并将其写入响应流来触发下载。同样,...
`resetBuffer()`可以清空缓冲区,`isCommitted()`检查是否已提交响应。 9. **关闭响应** 一旦响应被提交,就不能更改。但可以通过`reset()`来清除所有设置,使响应回到初始状态。然而,一旦客户端开始接收数据,就...
response.flushBuffer(); } catch (Exception e) { e.printStackTrace(); } finally { // 清理资源 } %> ``` 2. **采用文件流输出的方式下载** 文件流输出的方法更为直接,它涉及读取文件内容并将其写入...
response.setContentLength(buffer.length); OutputStream os = response.getOutputStream(); os.write(buffer); os.flush(); os.close(); fis.close(); ``` 以上代码片段展示了如何设置Servlet来提供文件下载。它...
1. **重置响应**:虽然不是必须的,但通常会先调用`response.reset()`来清除任何已存在的输出流或writer,确保后续操作的正确性。 2. **设置响应头**:与第一种方法类似,设置响应类型和文件名: ```jsp ...
response.reset(); response.setContentType(conn.getContentType()); response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(param.get("name").toString(), "UTF-8")); ...
response.reset(); response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes())); response.addHeader("Content-Length", "" + file.length()); OutputStream to...
response.reset(); response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); File file = new File(filePath); ...
response.reset(); // 设置响应头 response.setContentType("application/x-download"); // 设置文件名编码,确保非英文文件名正确显示 response.setHeader("Content-disposition", "attachment; filename=" +...
response.reset(); e.printStackTrace(); } finally { // ...更多流关闭逻辑... } return null; } ``` #### 结论 通过上述步骤和代码示例,我们可以清晰地了解到如何使用Java实现文件下载功能。这种能力...
response.reset(); response.setContentType("bin"); response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); // 循环取出流中的数据 byte[] b = new byte[100]; int len;...
response.reset(); response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); File file = new File(savePath); // ...
response.reset(); ``` 设置响应头,指示浏览器以附件形式下载文件,并指定文件名: ```java response.addHeader("Content-Disposition", "attachment;filename=" + new String(zipName.getBytes())); ``` 这里...
response.reset(); response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\""); OutputStream out = response....
response.reset(); response.setContentType("application/octet-stream;charset=UTF-8"); response.setHeader("Content-Disposition", "attachment; filename=" + filename); response.setContentLength((int) file...
response.reset(); response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); FileInputStream fis = new ...