下载程序运行两次,第一次必然报错:getOutputStream() has already been called for this response java下载报错:getOutputStream() has...

\u6c42\u89e3\uff01\uff01\uff01JSF\u5b9e\u73b0\u4e0b\u8f7d\u529f\u80fd\u8001\u662f\u62a5\u8fd9\u4e2a\u9519\uff0cgetOutputStream() has already been called for this response\u3002

\u95ee\u9898\uff1a\u4f60\u9700\u8981\u6e05\u7a7a\u5e76\u4e14\u5173\u95ed\u6d41\u5bf9\u8c61\u3002
\u6211\u63d0\u4f9b\u4e24\u79cd\u89e3\u51b3\u65b9\u6848\uff0c\u5e76\u4e14\u89e3\u51b3\u4e86\u4e2d\u6587\u6587\u4ef6\u540d\u4e71\u7801\u95ee\u9898\uff0c\u517c\u5bb9IE\u548c\u706b\u72d0\u4e24\u79cd\u6d4f\u89c8\u5668\u3002
\u4f60\u53ef\u4ee5\u9009\u62e9\u3002
\u65b9\u6848\u4e00\uff1a\u4ee3\u7801\u5199\u5728\u4e00\u4e2a.jsp\u6587\u4ef6\u4e2d\u3002

\u4e0b\u8f7d\u8bf7\u6c42\u9875\u9762\u4f20\u9012\u8fc7\u6765\u7684\u53c2\u6570\uff08\u5f53\u7136\u5177\u4f53\u770b\u4f60\u8981\u51e0\u4e2a\u53c2\u6570\uff09










file.jsp \u6587\u4ef6\u4ee3\u7801\uff1a


<%response.reset();
String ATTACHMENTSTOREPATH = \u5df2\u7ecf\u4e0a\u4f20\u4e86\u7684\u9644\u4ef6\u7684\u8def\u5f84\uff0c\u81ea\u5df1\u5e94\u8be5\u77e5\u9053\uff1f
int docId = 0;
int revNo = -1;
int attachmentNo = 0;
String filename = null;
String storageName = null;

try {
docId = Integer.parseInt(request.getParameter("docId"));
revNo = Integer.parseInt(request.getParameter("revNo"));
attachmentNo = Integer.parseInt(request.getParameter("attachmentNo"));
//\u5728\u706b\u72d0\u6d4f\u89c8\u5668\u4e0b\u8f7d\uff0c\u542b\u7a7a\u683c\u7684\u6587\u4ef6\u540d\u4f1a\u51fa\u73b0\u5f02\u5e38\uff0c\u4e8e\u662f\u5c06\u7a7a\u683c\u7528\u4e0b\u5212\u7ebf\u4ee3\u66ff
filename = (new String(request.getParameter("filename").
getBytes("ISO-8859-1"),"UTF-8")).replace(" ", "_");

//\u5c06\u7a7a\u683c\u8f6c\u5316\u4e3a\u4e0b\u5212\u7ebf\u540e\u91cd\u65b0\u5bf9\u6587\u4ef6\u540d\u8fdb\u884cUTF-8\u7f16\u7801
filename = java.net.URLEncoder.encode(filename, "UTF-8"); storageName = request.getParameter("storageName");
} catch (NumberFormatException nfe) {

}
if (docId >= 0 && revNo > -1 && attachmentNo >= 0) {
String filePath = null;
filePath = ATTACHMENTSTOREPATH + File.separator + storageName;
response.setContentType("application/octet-stream");
//\u56e0\u4e3a\u6d4f\u89c8\u5668\u4f1a\u5c06\u5b57\u7b26GBK\u7f16\u7801\uff0c\u6240\u4ee5\u4ece\u6570\u636e\u5e93\u83b7\u5f97\u7684UTF-8\u9700\u8981\u8f6c\u6362\u6210GBK
//UTF-8\u4e00\u4e2a\u6c49\u5b5724\u4f4d\uff0cGBK\u4e00\u4e2a\u6c49\u5b5716\u4f4d
//Start UTF-8 to GBK(\u76f8\u5bf9\u4ee5\u524d\u7684\u7a0b\u5e8f\u4e3b\u8981\u5c31\u662f\u4fee\u6539\u4e86\u8fd9\u91cc\uff09
String str=filename;
StringBuffer sb = new StringBuffer();
for(int i=0; i<str.length(); i++) {
char c = str.charAt(i);
switch (c) {
case '+':
sb.append(' ');
break;
case '%':
try {
sb.append((char)Integer.parseInt(
str.substring(i+1,i+3),16));
}
catch (NumberFormatException e) {
throw new IllegalArgumentException();
}
i += 2;
break;
default:
sb.append(c);
break;
}
}
String result = sb.toString();
result= new String(result.getBytes("ISO-8859-1"),"UTF-8");
response.addHeader("Content-Disposition", "attachment; filename=" + new String(result.getBytes("GBK"),"ISO-8859-1"));
//End UTF-8 to GBK
BufferedOutputStream bos = null;
BufferedInputStream bis = null;
try {
bos = new BufferedOutputStream(response.getOutputStream());
bis = new BufferedInputStream(new FileInputStream(filePath));
byte[] buffer = new byte[1024];
int n = -1;
while ((n = bis.read(buffer)) > -1) {
bos.write(buffer, 0, n);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bis != null)
bis.close();
if (bos != null){
out.clear(); out = pageContext.pushBody(); bos.close(); } bis = null;
bos = null;
}
}
%>
\u6b64\u65b9\u6848\u4e2d\u5982\u679c\u4f60\u53bb\u6389\uff1aout.clear();
out = pageContext.pushBody();
\u62a5\u7684\u9519\u5c06\u548c\u4f60\u7684\u4e00\u6a21\u4e00\u6837\uff0c\u4e0d\u4fe1\u4f60\u8bd5\u8bd5\uff1f

\u65b9\u6848\u4e8c\uff1a\u7528Servlet\u4e0b\u8f7d\uff0c\u548c\u4f60\u7684\u662f\u4e00\u6837\u7684\u3002
\u8fd8\u662f\u9875\u9762\u8fc7\u6765\u7684\u53c2\u6570\uff1a \u5176\u4e2d/***/***\u662f\u4f60\u8c03\u7528\u7684Servlet









public class PrFileServlet extends HttpServlet {
//stauts\u5224\u65ad\u4f7f\u7528\u54ea\u4e2a\u8def\u5f84
private String UPLOAD_TEMP_PATH = "\u4e34\u65f6\u8def\u5f84\uff08\u8fd8\u6ca1\u6709\u4fdd\u5b58\uff09";
private String UPLOAD_PATH = "\u6587\u4ef6\u8def\u5f84\uff08\u5df2\u7ecf\u4fdd\u5b58\u4e86\u6587\u4ef6\uff09";

protected void doGet(HttpServletRequest request, HttpServletResponse response)throws IOException{
String storageName =
(request.getParameter("storage")==null?"":request.getParameter("storage"));
//\u5728\u706b\u72d0\u6d4f\u89c8\u5668\u4e0b\u8f7d\uff0c\u542b\u7a7a\u683c\u7684\u6587\u4ef6\u540d\u4f1a\u51fa\u73b0\u5f02\u5e38\uff0c\u4e8e\u662f\u5c06\u7a7a\u683c\u7528\u4e0b\u5212\u7ebf\u4ee3\u66ff
String displayName =new String(request.getParameter("display").getBytes("ISO-8859-1"),"UTF8").replace(" ", "_");
//\u5c06\u7a7a\u683c\u8f6c\u5316\u4e3a\u4e0b\u5212\u7ebf\u540e\u91cd\u65b0\u5bf9\u6587\u4ef6\u540d\u8fdb\u884cUTF-8\u7f16\u7801
displayName = java.net.URLEncoder.encode(displayName, "UTF-8");
String status = (request.getParameter("status")==null?"":request.getParameter("status"));
String code = (request.getParameter("code")==null?"":request.getParameter("code"));
String filePath = "";
if (status.equals("N")) {
filePath = UPLOAD_PATH + "//" + storageName.substring(0, 6);
} else if (status.equals("T")) {
filePath = UPLOAD_TEMP_PATH;
}
File file = new File(filePath, storageName);
if (file == null) {
response.sendRedirect("/weberp/workflow/pr/fileNotFound.jsp");
return;
}
if (!file.exists()) {
response.sendRedirect("/weberp/workflow/pr/fileNotFound.jsp");
return;
}
String contentType = getServletContext().getMimeType(storageName);
if (contentType == null) {
contentType = "application/octet-stream";
}
BufferedInputStream input = null;
BufferedOutputStream output = null;

try {
input = new BufferedInputStream(new FileInputStream(file));
int contentLength = input.available();

// Init servlet response.
response.reset();
response.setContentType(contentType);
response.setContentLength(contentLength);
//\u56e0\u4e3a\u6d4f\u89c8\u5668\u4f1a\u5c06\u5b57\u7b26GBK\u7f16\u7801\uff0c\u6240\u4ee5\u4ece\u6570\u636e\u5e93\u83b7\u5f97\u7684UTF-8\u9700\u8981\u8f6c\u6362\u6210GBK
//UTF-8\u4e00\u4e2a\u6c49\u5b5724\u4f4d\uff0cGBK\u4e00\u4e2a\u6c49\u5b5716\u4f4d
//Start UTF-8 to GBK(\u76f8\u5bf9\u4ee5\u524d\u7684\u7a0b\u5e8f\u4e3b\u8981\u5c31\u662f\u4fee\u6539\u4e86\u8fd9\u91cc\uff09
String str=displayName;
StringBuffer sb = new StringBuffer();
for(int i=0; i<str.length(); i++) {
char c = str.charAt(i);
switch (c) {
case '+':
sb.append(' ');
break;
case '%':
try {
sb.append((char)Integer.parseInt(
str.substring(i+1,i+3),16));
}
catch (NumberFormatException e) {
throw new IllegalArgumentException();
}
i += 2;
break;
default:
sb.append(c);
break;
}
}
String result = sb.toString();
result= new String(result.getBytes("ISO-8859-1"),"UTF-8");
response.setHeader("Content-Disposition", "attachment; filename=" + new String(result.getBytes("GBK"),"ISO-8859-1"));
//End UTF-8 to GBK
output = new BufferedOutputStream(response.getOutputStream());

// Write file contents to response.
for (int data; (data = input.read()) != -1;) {
output.write(data);
}

// Finalize task.
output.flush();
} catch (IOException e) {
// Something went wrong?
e.printStackTrace();
} finally {
// Gently close streams.
close(output);
close(input);
}
}

private static void close(Closeable resource) {
if (resource != null) {
try {
resource.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
\u597d\u597d\u770b\u61c2\uff0c\u8981\u6709\u8010\u5fc3\u3002\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\u3002

\u3000\u3000\u51fa\u73b0getOutputStream() has already been called for this response\u5f02\u5e38\u7684\u539f\u56e0\u548c\u89e3\u51b3\u65b9\u6cd5\uff1a
\u3000\u3000\u51fa\u73b0\u6b64\u9519\u8bef\u4e00\u822c\u90fd\u662f\u5728jsp\u4e2d\u4f7f\u7528\u4e86\u8f93\u51fa\u6d41\uff08\u5982\u8f93\u51fa\u56fe\u7247\u9a8c\u8bc1\u7801\uff0c\u6587\u4ef6\u4e0b\u8f7d\u7b49\uff09\uff0c\u6ca1\u6709\u59a5\u5584\u5904\u7406\u597d\u7684\u539f\u56e0\u3002
\u3000\u3000\u89e3\u51b3\u7684\u529e\u6cd5\uff0c\u5728\u4f7f\u7528\u5b8c\u8f93\u51fa\u6d41\u4ee5\u540e\u8c03\u7528\u4ee5\u4e0b\u4e24\u884c\u4ee3\u7801\u5373\u53ef\uff1a
\u3000
\u3000 out.clear();
\u3000\u3000out = pageContext.pushBody();

getOutputStream() has already been called for this response异常出现的原因和解决方法:
jsp中出现此错误一般都是在jsp中使用了输出流(如输出图片验证码,文件下载等),没有妥善处理好的原因。

具体的原因:jsp编译成servlet之后在函数
_jspService(HttpServletRequest request, HttpServletResponse response)

的最后
有一段这样的代码
Java代码 收藏代码
finally {
if (_jspxFactory != null)
_jspxFactory.releasePageContext(_jspx_page_context);
}

这里是在释放在jsp中使用的对象,会调用response.getWriter(),因为这个方法是和response.getOutputStream()相冲突的!所以会出现以上这个异常。然后当然是要提出解决的办法,其实挺简单的,在使用完输出流以后调用以下两行代码即可:

out.clear();
out = pageContext.pushBody();

  • 娴锋嘲瀹㈣Е鎽稿睆绋嬪簭涓嬭浇鏃舵姤绛夊緟瓒呮椂,鏄粈涔堝師鍥?
    绛旓細缃戠粶涓嶅ソ銆傚綋缃戠粶涓嶅ソ鏃讹紝涔熷氨鏄綉缁滄湭杩炴帴銆佺綉缁滆繛鎺ラ熷害鎱紝缃戠粶杩炴帴澶辫触绛夊師鍥狅紝瀵艰嚧鏈兘姝e父涓庝俊鍙峰熀绔欒繘琛岄氳鑾峰緱鏁版嵁锛岃繘鑰屽鑷存捣娉板瑙︽懜灞绋嬪簭涓嬭浇鏃舵姤绛夊緟瓒呮椂锛岀瓑寰呯綉缁滆壇濂藉嵆鍙
  • 涓轰粈涔堢數鑴戞墦寮搴旂敤绋嬪簭鐨勬椂鍊欒佹槸鍑虹幇瀹夊叏璀︽姤?
    绛旓細浣犲ソ锛岀數鑴戞墦寮搴旂敤绋嬪簭浼氭彁绀哄畨鍏ㄨ鎶ユ湁涓や釜涓昏鍘熷洜锛氫竴銆佹槸鍥犱负浣犳墍鐢ㄧ殑搴旂敤绋嬪簭涓庣郴缁熷瓨鍦ㄥ吋瀹规ч棶棰樸備簩銆佹槸浣犵殑搴旂敤绋嬪簭鍚姩鏃朵細璁块棶鎴栬皟鐢ㄧ郴缁熷唴闇瑕佹洿楂樻潈闄愭墠鑳借闂殑绯荤粺鏂囦欢銆備竴鑸彧瑕佹槸鑷繁淇′换鐨勭▼搴忓彧瑕佹寜鎻愮ず鐐圭‘瀹氬氨濂斤紝褰撶劧濡傛灉鏄竴浜涜嚜宸遍兘涓嶆竻妤氫笉鏄庣櫧鐨勫氨鐐瑰惁銆
  • 鎴戠敤st-link涓嬭浇绋嬪簭鍒皊tm32,绗竴娆寰堟垚鍔,浣嗙浜屾灏变笉鑳戒笅杞戒簡
    绛旓細浣犳妸SW寮曡剼缁欑敤浜嗭紝鍙互鍦ㄥ揩閫熸寜涓嬪浣嶉敭鐨勫悓鏃涓嬭浇绋嬪簭
  • ...褰曞埗浜嗚剼鏈箣鍚,閲嶅杩愯妯″紡,杩愯绗竴娆鐨勬椂鍊欐甯,鍒颁簡绗簩娆...
    绛旓細鎶婁綘瑕佸仛鐨勬暣濂楀姩浣滈氳繃浠g爜鍐欏嚭鏉ュ氨绋冲畾浜.涓鑸兘閫氳繃褰曞埗瑙e喅鐨勯棶棰橀兘鏄姩浣滈潪甯哥畝鍗曠殑,閭d箞鏋勫缓涓濂椾唬鐮蹇呯劧涔熼潪甯哥畝鍗.
  • 鏄嗕粦閫氭嘲涓嬭浇绋嬪簭鎶鍐呭瓨涓嶈冻
    绛旓細1銆佸叧闂叾浠栫▼搴忥細璁$畻鏈哄悓鏃杩愯浜嗗涓▼搴忥紝鍙互灏濊瘯鍏抽棴涓浜涗笉蹇呰鐨勭▼搴忥紝閲婃斁鍐呭瓨璧勬簮銆2銆佸鍔犲唴瀛橈細璁$畻鏈哄唴瀛樹笉瓒筹紝鍙互鑰冭檻澧炲姞鍐呭瓨鏉★紝鎻愰珮璁$畻鏈虹殑鍐呭瓨瀹归噺銆3銆侀噸鏂颁笅杞界▼搴忥細涓嬭浇绋嬪簭鏈韩瀛樺湪闂锛屽彲浠ュ皾璇曢噸鏂涓嬭浇绋嬪簭锛鎴栬呬粠鍏朵粬娓犻亾涓嬭浇銆
  • 涓轰粈涔圕++缂栧啓鐨绋嬪簭绗竴娆¤繍琛娌¢敊,涔嬪悗杩愯灏卞嚭鐜 鍑虹幇绋嬪簭閿欒
    绛旓細鍙兘鏄浜娆¤繍琛鏃讹紝杩欏彞娌℃垚鍔燂紝鑰屼綘娌℃湁鍒ゆ柇灏辩洿鎺ユ搷浣滄枃浠讹紝鎵浠ュ嚭鐜版湭鐭ラ敊璇紝浣犲湪杩欓噷璁句竴涓柇鐐圭湅鐪嬨俧p=fopen("data2.txt","w+");
  • ...涓浼氬氨鎻愮ず绋嬪簭宕╂簝 浣嗛噸鍚竴涓灏卞ソ浜 瑕佹槸绗竴娆寮浜嗕互鍚庝笉鍔 涓浼...
    绛旓細鎴戠數鑴戞瘡娆″紑鏈轰竴鎵撳紑绋嬪簭 绛変竴浼氬氨鎻愮ず绋嬪簭宕╂簝 浣嗛噸鍚竴涓灏卞ソ浜 瑕佹槸绗竴娆寮浜嗕互鍚庝笉鍔 涓浼氬氨...2.杩愯鐢辫绠楁満鍒堕犲晢鎻愪緵鐨勬墍鏈夌郴缁熻瘖鏂蒋浠.灏ゅ叾鏄唴瀛樻鏌. 3.妫鏌ユ槸鍚︽纭畨瑁呬簡鎵鏈夋柊纭欢鎴栬蒋浠...C寮澶寸殑鏁呴殰,瀹冩姤鐨勯敊寰堥偑涔,鎶鏈煡纭欢鏁呴殰,涓嶈繃鍑鸿繖绫籆寮澶寸殑浠g爜澶氬崐涓庣‖浠舵棤鍏炽傚熀鏈槸涓庣郴缁...
  • 闃块噷鏃烘椇鑰佹槸鏄剧ず閿欒鎶ュ憡绋嬪簭,鍗歌浇閲嶆柊瀹夎涔熸槸涓鏍,鍝綅楂樻墜鎳傚緱?
    绛旓細濡傛灉鍙槸杩愯涓埆杞欢鍋跺皵鍑虹幇涓涓ゆ鐨勮瘽锛岀偣鈥滃彇娑堚濆嵆鍙紝鎴栭噸鍚數鑴戝啀璇曘傛瘮杈冮绻佺殑鍑虹幇鐢氳嚦褰卞搷鐢佃剳姝e父浣跨敤鐨勮瘽锛岃鑰冭檻閲嶆柊涓嬭浇瀹夎绋嬪簭鎴栭噸鍋氱郴缁熴傚嚭鐜拌繖涓彁绀轰竴鑸兘鏄綘鎵涓嬭浇鐨勮蒋浠朵笌鐢佃剳鍐呭瓨鏈夆滃啿绐佲濓紝鍘熷洜鎬荤粨璧锋潵鏈変互涓嬫柟闈細1銆佷綘涓嬭浇鐨勬椇鏃虹▼搴忎笉绋冲畾锛屾垨鑰呯増鏈お鏃с傝В鍐冲姙娉曪細鍙互鍏堝交搴...
  • 鐢↗ava鍐欎簡涓簲鐢绋嬪簭,杩愯涓娆闇瑕佸娆¤闂綉绔, 杩愯鍚庡啀娆℃墦寮璁块棶...
    绛旓細403閿欒鏄綉绔欐嫆缁濅簡浣犵殑璁块棶锛屾渶鏈夊彲鑳界殑鏄洜涓轰綘杩炵画璁块棶澶瘑闆嗭紝瓒呰繃浜嗘椂闂撮檺鍒讹紝缃戠珯浼氭嫆缁濅綘鐨勮闂傞櫎闈炰綘鑳借缃戠珯鏀瑰彉鍙楄瑙勫垯锛屼笉鐒跺氨鍘讳紭鍖栦綘鐨勪唬鐮佸惂锛屼笉瑕佽繛缁闂
  • 鐢佃剳涓杩愯绋嬪簭澶辫触鍑虹幇杩愯搴撻敊璇殑闂瑙e喅姹囨
    绛旓細鍙兘浼氫笉灏戠敤鎴峰湪鐢佃剳涓亣鍒颁簡杩愯绋嬪簭鍚,鎻愮ず閿欒鏃犳硶鎵撳紑鐨勬儏鍐,鑰屽叾涓彁绀虹殑閿欒鍙堟湁鎵涓嶅悓,鍙兘鏄蒋浠舵湰韬畨瑁呬笉瀹屾暣,鎴栬蒋浠朵笉鍏煎绯荤粺绛夈傚湪鎺ヤ笅鏉ョ殑鍐呭涓,灏忕紪瑕佸拰澶у浠嬬粛鍦ㄧ數鑴戜腑杩愯绋嬪簭澶辫触鍑虹幇杩愯搴撻敊璇殑闂瑙e喅姹囨汇傛帹鑽:windows鎿嶄綔绯荤粺涓嬭浇鍏蜂綋鏂规硶濡備笅:涓銆佺己灏慸3dx9_xx.dll鎵撳紑涓浜涙父鎴,鍙兘浼氭敹...
  • 扩展阅读:记忆力减退的原因 ... 程序已运行但是没窗口 ... 怎么关掉运行的程序 ... 手机后台运行怎么关闭 ... 软件运行但看不见窗口 ... 电脑老是出现程序停止 ... 手机后台运行在哪里看 ... 文件正被另一程序占用 ... 记忆力下降是什么原因引起的 ...

    本站交流只代表网友个人观点,与本站立场无关
    欢迎反馈与建议,请联系电邮
    2024© 车视网