用struts2开发了一个文件下载功能,IE,FIREFOX都可以,可是360就不能下载。显示错误
2014-1-28 10:20:31 com.opensymphony.xwork2.util.logging.commons.CommonsLogger error
严重: Can not find a java.io.InputStream with the name [fileInputStream] in the invocation stack. Check the <param name="inputName"> tag specified for this action.
struts2.xml:
<action name="muxz_action2" class="updownfiles.DownFilesAction" >
<result name="success" type="stream">
<!-- 下载文件类型定义 -->
<param name="contentType">${mimeType}</param>
<!-- 下载文件处理方法 -->
<param name="contentDisposition">
attachment;filename="${downFileName}"
</param>
<!-- 下载文件输出流定义 getFileInputStream() -->
<param name="inputName">fileInputStream</param>
<param name="bufferSize">4096</param>
</result>
</action>
action:(我的文件存放在数据库里)
public InputStream getFileInputStream() throws Exception {
return fileInputStream;
}
public String getMimeType() {
return ServletActionContext.getServletContext().getMimeType(downFileName);
}
public String getDownFileName() {
String FileName = downFileName;
try {
//对文件名转码, 不然在下载的时候文件名是乱码.
if(ServletActionContext.getRequest().getHeader("USER-AGENT").toLowerCase().indexOf("msie") > 0 ? true : false){
FileName = new String(FileName.getBytes("GBK"), "ISO-8859-1");
}else{
FileName = new String(FileName.getBytes("UTF-8"), "ISO-8859-1");
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return FileName;
}
public void setDownFileName(String downFileName) {
this.downFileName = downFileName;
}
public String execute() throws Exception {
conn=new Dbconn2().conn();
try {
sql = "select thefile,fileName,extname from mis.chsi_tmp_files where id='"
+ id + "'";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
Blob blob=rs.getBlob("thefile");
downFileName=rs.getString("fileName")+"."+rs.getString("extname");
fileInputStream=new BufferedInputStream(blob.getBinaryStream());
byte[]bytes =new byte[(int)blob.length()];
int len=bytes.length;
int offset = 0;
int read = 0;
while (offset < len && (read =fileInputStream.read(bytes, offset,len)) >= 0) {
offset += read;
}
fileInputStream=new ByteArrayInputStream(bytes);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if(rs!=null){
rs.close();
}
if(stmt!=null){
stmt.close();
}
conn.close();
}
return SUCCESS;
}
------解决方案--------------------
contentLength这个参数最好也能设置一下。另外,contentType参数值MimeType你用代码获得的,如果不需要显示,必须要下载的话应该使用:application/octet-stream