struts2 下载问题 javadownloadJavaee 下载 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <param name="inputName">inputStream</param> 我后台的方法getDownloadFile() 框架返回的 是个 inputStream 流 ,你返回getDownloadFile 是个流吗? public InputStream getDownloadFile() throws FileNotFoundException, UnsupportedEncodingException{ fileN=new String(fileName.getBytes( "ISO8859-1" ), "utf-8" ); File file = new File("D:\\xmxmmyeclipe6.5\\Gjdw_admin\\WebRoot\\"+DOWNLOADFILEPATH+fileN); System.out.println("dddddddddddddddddddddd"+DOWNLOADFILEPATH+fileN); InputStream is = new FileInputStream(file); InputStream is = new FileInputStream(file); return is; } 返回的是流 <param name="inputName">downloadFile</param> public InputStream getDownloadFile() throws Exception { return ServletActionContext.getServletContext().getResourceAsStream(xxxx.jpg") ; } // public InputStream getDownloadFile() throws UnsupportedEncodingException {// fileN=new String(fileName.getBytes( "ISO8859-1" ), "utf-8" );// System.out.print( ServletActionContext.getServletContext().getResourceAsStream(DOWNLOADFILEPATH+fileN)+"/////////////大家后才是真的");// return // ServletActionContext.getServletContext().getResourceAsStream(DOWNLOADFILEPATH+fileN); // }我一开始写过 没用 的 这样也不行 报的错就是 Can not find a java.io.InputStream with the name [downloadFile] 没有流信息,你debug 测试,看看是否跑到你返回流的那句代码。 <action name="download" class="com.tzht.uploadmesAction.DownloadAction"> <!-- 设置文件名参数,由页面上传入 --> <param name="fileName"></param> <result name="success" type="stream"> 这个是不是 写在 result 中? <result name="success" type="stream"> <!-- 下载文件类型定义 --> <param name="contentType"> application/octet-stream </param> <!-- 下载文件处理方法 --> <param name="contentDisposition"> attachment;filename="${downloadChineseFileName}" </param> <!-- 下载文件输出流定义 --> <param name="inputName">downloadFile</param> <param name="bufferSize">4096</param> <param name="contentLentgth">4096</param> </result> 他们已经在一个大result中了呀 /** * @see org.apache.struts2.dispatcher.StrutsResultSupport#doExecute(java.lang.String, com.opensymphony.xwork2.ActionInvocation) */ protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception { // Override any parameters using values on the stack resolveParamsFromStack(invocation.getStack(), invocation); OutputStream oOutput = null; try { if (inputStream == null) { // Find the inputstream from the invocation variable stack inputStream = (InputStream) invocation.getStack().findValue(conditionalParse(inputName, invocation)); } if (inputStream == null) { String msg = ("Can not find a java.io.InputStream with the name [" + inputName + "] in the invocation stack. " + "Check the <param name=\"inputName\"> tag specified for this action."); LOG.error(msg); throw new IllegalArgumentException(msg); } // Find the Response in context HttpServletResponse oResponse = (HttpServletResponse) invocation.getInvocationContext().get(HTTP_RESPONSE); // Set the content type if (contentCharSet != null && ! contentCharSet.equals("")) { oResponse.setContentType(conditionalParse(contentType, invocation)+";charset="+contentCharSet); } else { oResponse.setContentType(conditionalParse(contentType, invocation)); } // Set the content length if (contentLength != null) { String _contentLength = conditionalParse(contentLength, invocation); int _contentLengthAsInt = -1; try { _contentLengthAsInt = Integer.parseInt(_contentLength); if (_contentLengthAsInt >= 0) { oResponse.setContentLength(_contentLengthAsInt); } } catch(NumberFormatException e) { LOG.warn("failed to recongnize "+_contentLength+" as a number, contentLength header will not be set", e); } } // Set the content-disposition if (contentDisposition != null) { oResponse.addHeader("Content-Disposition", conditionalParse(contentDisposition, invocation)); } // Set the cache control headers if neccessary if (!allowCaching) { oResponse.addHeader("Pragma", "no-cache"); oResponse.addHeader("Cache-Control", "no-cache"); } // Get the outputstream oOutput = oResponse.getOutputStream(); if (LOG.isDebugEnabled()) { LOG.debug("Streaming result [" + inputName + "] type=[" + contentType + "] length=[" + contentLength + "] content-disposition=[" + contentDisposition + "] charset=[" + contentCharSet + "]"); } // Copy input to output LOG.debug("Streaming to output buffer +++ START +++"); byte[] oBuff = new byte[bufferSize]; int iSize; while (-1 != (iSize = inputStream.read(oBuff))) { oOutput.write(oBuff, 0, iSize); } LOG.debug("Streaming to output buffer +++ END +++"); // Flush oOutput.flush(); } finally { if (inputStream != null) inputStream.close(); if (oOutput != null) oOutput.close(); } } 奥 我明白了 但是我debug确实有对象呀java.io.FileInputStream@1ddc3ea没法传图 请问有何见解呀 连接1 连接2现在就是你的流没返回,contentLength 这个是返回的需要配吗 。 本来我没配 在网上查的加上了 不好试 没去 还有好像txt的文件可以下载 word,exel好行就不能 真怪 就报这个 别的没有了 严重 Can not find a java.io.InputStream with the name [downloadFile] in the invocation stack. Check the <param name="inputName"> tag specified for this action. spring到底是个什么概念啊 j2ee中回调? 有关jdom中element为ROOT的处理方法 关于水晶报表的问题(Crystal Report) hibernate一对多双向关联到问题!!! java .util.Date问题 90分求一SQL语句,成功立刻结帖! 我要在java中生成、解析xml文档,请问相关接口是什么,其说明在哪里有? springmvc中如何遍历一个对象! eclipse打不开 关于maven打包war 出现“找不到符号” 错误 struts2下载的问题
fileN=new String(fileName.getBytes( "ISO8859-1" ), "utf-8" );
File file = new File("D:\\xmxmmyeclipe6.5\\Gjdw_admin\\WebRoot\\"+DOWNLOADFILEPATH+fileN);
System.out.println("dddddddddddddddddddddd"+DOWNLOADFILEPATH+fileN);
InputStream is = new FileInputStream(file);
InputStream is = new FileInputStream(file);
return is;
} 返回的是流
{
return ServletActionContext.getServletContext().getResourceAsStream(xxxx.jpg") ;
}
// fileN=new String(fileName.getBytes( "ISO8859-1" ), "utf-8" );
// System.out.print( ServletActionContext.getServletContext().getResourceAsStream(DOWNLOADFILEPATH+fileN)+"/////////////大家后才是真的");
// return
// ServletActionContext.getServletContext().getResourceAsStream(DOWNLOADFILEPATH+fileN);
// }我一开始写过 没用 的 这样也不行
<!-- 设置文件名参数,由页面上传入 -->
<param name="fileName"></param>
<result name="success" type="stream"> 这个是不是 写在 result 中?
<!-- 下载文件类型定义 -->
<param name="contentType">
application/octet-stream
</param>
<!-- 下载文件处理方法 -->
<param name="contentDisposition">
attachment;filename="${downloadChineseFileName}"
</param>
<!-- 下载文件输出流定义 -->
<param name="inputName">downloadFile</param>
<param name="bufferSize">4096</param>
<param name="contentLentgth">4096</param>
</result> 他们已经在一个大result中了呀
* @see org.apache.struts2.dispatcher.StrutsResultSupport#doExecute(java.lang.String, com.opensymphony.xwork2.ActionInvocation)
*/
protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception { // Override any parameters using values on the stack
resolveParamsFromStack(invocation.getStack(), invocation); OutputStream oOutput = null; try {
if (inputStream == null) {
// Find the inputstream from the invocation variable stack
inputStream = (InputStream) invocation.getStack().findValue(conditionalParse(inputName, invocation));
} if (inputStream == null) {
String msg = ("Can not find a java.io.InputStream with the name [" + inputName + "] in the invocation stack. " +
"Check the <param name=\"inputName\"> tag specified for this action.");
LOG.error(msg);
throw new IllegalArgumentException(msg);
} // Find the Response in context
HttpServletResponse oResponse = (HttpServletResponse) invocation.getInvocationContext().get(HTTP_RESPONSE); // Set the content type
if (contentCharSet != null && ! contentCharSet.equals("")) {
oResponse.setContentType(conditionalParse(contentType, invocation)+";charset="+contentCharSet);
}
else {
oResponse.setContentType(conditionalParse(contentType, invocation));
} // Set the content length
if (contentLength != null) {
String _contentLength = conditionalParse(contentLength, invocation);
int _contentLengthAsInt = -1;
try {
_contentLengthAsInt = Integer.parseInt(_contentLength);
if (_contentLengthAsInt >= 0) {
oResponse.setContentLength(_contentLengthAsInt);
}
}
catch(NumberFormatException e) {
LOG.warn("failed to recongnize "+_contentLength+" as a number, contentLength header will not be set", e);
}
} // Set the content-disposition
if (contentDisposition != null) {
oResponse.addHeader("Content-Disposition", conditionalParse(contentDisposition, invocation));
} // Set the cache control headers if neccessary
if (!allowCaching) {
oResponse.addHeader("Pragma", "no-cache");
oResponse.addHeader("Cache-Control", "no-cache");
} // Get the outputstream
oOutput = oResponse.getOutputStream(); if (LOG.isDebugEnabled()) {
LOG.debug("Streaming result [" + inputName + "] type=[" + contentType + "] length=[" + contentLength +
"] content-disposition=[" + contentDisposition + "] charset=[" + contentCharSet + "]");
} // Copy input to output
LOG.debug("Streaming to output buffer +++ START +++");
byte[] oBuff = new byte[bufferSize];
int iSize;
while (-1 != (iSize = inputStream.read(oBuff))) {
oOutput.write(oBuff, 0, iSize);
}
LOG.debug("Streaming to output buffer +++ END +++"); // Flush
oOutput.flush();
}
finally {
if (inputStream != null) inputStream.close();
if (oOutput != null) oOutput.close();
}
}
连接2
现在就是你的流没返回,contentLength 这个是返回的需要配吗 。