JspSmartUpload下载报错

JspSmartUpload下载报错getOutputStream() has already been called for this response
楼主hony1999()2004-09-12 16:15:21 在 Java / Web 开发 提问
我使用JspSmartUpload控件下载的时候,使用
[quote]mySmartUpload.downloadFile(downloadpath); [/quote]
有的可下载,有的会提示出错:
[code] java.lang.IllegalStateException: getOutputStream() has already been called for this response [/code]
怎么回事啊?我想是控件内部打开了某个变量,然后打开了没有关闭,但没有源文件,也不知道怎样改
[code] <%
mySmartUpload.init(config);
mySmartUpload.service(request,response);
String username=request.getParameter("author");
String file=request.getParameter("flname");

mySmartUpload.downloadFile("/upload/"+username+"/"+file);
%> [/code] 有人遇过这个错误吗?这里面还要添加什么语句啊

问题点数:40、回复次数:10
Top


1 楼hony1999()回复于 2004-09-13 10:20:51 得分 0 怎么没人理我
Top

2 楼KarlL(只想小鱼)回复于 2004-09-16 12:06:12 得分 10我也是用这个mySmartupload,请加我karl2003B@msn.com
Top

3 楼qldong(董)回复于 2004-10-27 20:11:21 得分 0 也想知道!
Top

4 楼samue(下弦月)回复于 2004-11-22 22:46:13 得分 5我也是这样的问题,在下TXT文件的时候

Top

5 楼Herman_TheTemplar(下雨的天空)回复于 2004-11-23 08:43:56 得分 5不光是下载文件,凡是涉及输出流的,都遇到过这个问题。
关注!!!
你用“java.lang.IllegalStateException: getOutputStream() has already been called for this response”在Google能查出一堆来
谁能帮我们真正解决一下……
Top

6 楼Herman_TheTemplar(下雨的天空)回复于 2004-11-24 09:09:14 得分 0 解决了吗?
Top

7 楼tangliyingtly(小汤)回复于 2004-11-24 13:42:17 得分 0 也遇到这个问题,求助!
Top

8 楼fengliu212()回复于 2004-11-24 16:01:41 得分 0 关注
Top

9 楼cnfalcon(中国猎鹰)回复于 2004-11-25 10:07:56 得分 20下载时最好使用servlet,用jsp编译后加入下面的代码
[code] _jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out;
其中out = pageContext.getOut();就是得到OutputStream,运行时就出错了,用servlet不会出现这种问题。
代码
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import com.jspsmart.upload.*;

public class servletDown extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";

private ServletConfig config;
/**
* Init the servlet
*/
final public void init(ServletConfig config) throws ServletException {
this.config = config;
}

final public ServletConfig getServletConfig() {
return config;
}

//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
SmartUpload mySmartUpload=new SmartUpload();
// Initialization
mySmartUpload.initialize(config,request,response);

try
{
// Download file
mySmartUpload.downloadFile("/upload/sample.zip");
}
catch(IOException ioe)
{
System.out.println("ioe");
System.out.println(ioe.getMessage());
}
catch(SmartUploadException sme)
{
System.out.println("sme");
System.out.println(sme.getMessage());
}
catch(ServletException se)
{
System.out.println("se");
System.out.println(se.getMessage());
}
}

//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}

//Clean up resources
public void destroy() {
}
}
[/code]Top

10 楼hony1999()回复于 2005-07-19 22:21:04 得分 0
[code]后来解决了,就是文件开始的<%%>都要紧挨着,不能有空格,更不能有换行,如:
<%@ page contentType="text/html; charset=GBK" %><%@ page import="java.io.*" %><%
真不知道为什么,但确实好使,哈哈,希望遇到同样问题的兄弟们都试试。[/code]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
解决jspsmartupload上传文件中文乱码 采用jspsmartupload上传文件时,如果文件名含有中文,在服务器端取得文件名是会出现乱码。如果表单项中填写了中文,一样会有乱码问题。看了下jspsmartupload的源码,改了两个地方,现在可以没有乱码问题了。 第一个地方,修改类SmartUpload中的upload()方法 // //这次用jspSmartUpload做文件上传下载, //该组件默认是GBK编码,当上传的文件名为中文时, //我将文件名getBytes()下,将GBK改成UTF-8。测试了下,貌似没问题, //突然有一次上传一文件时,发现最后几个字乱码,一直是??。在拿些文件测试, // 后来知道了是当文件名为中文奇数时,会乱码,而且还上传不了。 // 再做测试,找原因,查看字符的长度,转成16进制看结果。觉得是jspSmartUpload组件对中文支持不足的问题。 http://ru-yi86.javaeye.com/blog/368553 Java代码 1. public void upload() 2. throws SmartUploadException, IOException, ServletException 3. { 4. int totalRead = 0; 5. int readBytes = 0; 6. long totalFileSize = 0L; 7. boolean found = false; 8. String dataHeader = new String(); 9. String fieldName = new String(); 10. String fileName = new String(); 11. String fileExt = new String(); 12. String filePathName = new String(); 13. String contentType = new String(); 14. String contentDisp = new String(); 15. String typeMIME = new String(); 16. String subTypeMIME = new String(); 17. boolean isFile = false; 18. m_totalBytes = m_request.getContentLength(); 19. m_binArray = new byte[m_totalBytes]; 20. for(; totalRead < m_totalBytes; totalRead += readBytes) 21. try 22. { 23. m_request.getInputStream(); 24. readBytes = m_request.getInputStream().read(m_binArray, totalRead, m_totalBytes - totalRead); 25. } 26. catch(Exception e) 27. { 28. throw new SmartUploadException("Unable to upload."); 29. } 30. 31. for(; !found && m_currentIndex < m_totalBytes; m_currentIndex++) 32. if(m_binArray[m_currentIndex] == 13) 33. found = true; 34. else 35. m_boundary = m_boundary + (char)m_binArray[m_currentIndex]; 36. 37. if(m_currentIndex == 1) 38. return; 39. m_currentIndex++; 40. do 41. { 42. if(m_currentIndex >= m_totalBytes) 43. break; 44. dataHeader = getDataHeader(); 45. m_currentIndex = m_currentIndex + 2; 46. isFile = dataHeader.indexOf("filename") > 0; 47. fieldName = getDataFieldValue(dataHeader, "name"); 48. if(isFile) 49. { 50. filePathName = getDataFieldValue(dataHeader, "filename"); 51. fileName = getFileName(filePathName); 52. fileExt = getFileExt(fileName); 53. contentType = getContentType(dataHeader); 54. contentDisp = getContentDisp(dataHeader); 55. typeMIME = getTypeMIME(contentType); 56. subTypeMIME = getSubTypeMIME(contentType); 57. } 58. getDataSection(); 59. if(isFile && fileName.length() > 0) 60. { 61. if(m_deniedFilesList.contains(fileExt)) 62. throw new SecurityException("The extension of the file is denied to be uploaded (1015)."); 63. if(!m_allowedFilesList.isEmpty() && !m_allowedFilesList.contains(fileExt)) 64. throw new SecurityException("The extension of the file is not allowed to be uploaded (1010)."); 65. if(m_maxFileSize > (long)0 && (long)((m_endData - m_startData) + 1) > m_maxFileSize) 66. throw new SecurityException(String.valueOf((new StringBuffer("Size exceeded for this file : ")).append(fileName).append(" (1105)."))); 67. totalFileSize += (m_endData - m_startData) + 1; 68. if(m_totalMaxFileSize > (long)0 && totalFileSize > m_totalMaxFileSize) 69. throw new SecurityException("Total File Size exceeded (1110)."); 70. } 71. if(isFile) 72. { 73. com.jspsmart.upload.File newFile = new com.jspsmart.upload.File(); 74. newFile.setParent(this); 75. newFile.setFieldName(fieldName); 76. newFile.setFileName(fileName); 77. newFile.setFileExt(fileExt); 78. newFile.setFilePathName(filePathName); 79. newFile.setIsMissing(filePathName.length() == 0); 80. newFile.setContentType(contentType); 81. newFile.setContentDisp(contentDisp); 82. newFile.setTypeMIME(typeMIME); 83. newFile.setSubTypeMIME(subTypeMIME); 84. if(contentType.indexOf("application/x-macbinary") > 0) 85. m_startData = m_startData + 128; 86. newFile.setSize((m_endData - m_startData) + 1); 87. newFile.setStartData(m_startData); 88. newFile.setEndData(m_endData); 89. m_files.addFile(newFile); 90. } else 91. { 92. /** 93. * 原来的代码 94. * String value = new String(m_binArray, m_startData, (m_endData - m_startData) + 1); 95. */ 96. /** 97. * 解决取得request的参数的中文编码问题 98. */ 99. String value = new String(m_binArray, m_startData, (m_endData - m_startData) + 1, "utf-8"); 100. 101. 102. m_formRequest.putParameter(fieldName, value); 103. } 104. if((char)m_binArray[m_currentIndex + 1] == '-') 105. break; 106. m_currentIndex = m_currentIndex + 2; 107. } while(true); 108. } 第二个地方,修改类SmartUpload中的getDataHeader()方法 : Java代码 1. private String getDataHeader() 2. { 3. int start = m_currentIndex; 4. int end = 0; 5. int len = 0; 6. boolean found = false; 7. while(!found) 8. if(m_binArray[m_currentIndex] == 13 && m_binArray[m_currentIndex + 2] == 13) 9. { 10. found = true; 11. end = m_currentIndex - 1; 12. m_currentIndex = m_currentIndex + 2; 13. } else 14. { 15. m_currentIndex++; 16. } 17. 18. 19. //原始代码 20. //String dataHeader = new String(m_binArray, start, (end - start) + 1); 21. /** 22. * 解决文件名的中文乱码问题 23. */ 24. String dataHeader = null; 25. try { 26. dataHeader = new String(m_binArray, start, (end - start) + 1,"utf-8"); 27. } catch (UnsupportedEncodingException e) { 28. e.printStackTrace(); 29. } 30. return dataHeader; 31. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值