首先,我将一个文件作为BLOB类型上传到MySQL。我可以使用MySQL DB编辑器中的Open Value查看该文件的内容。然后我下载了该文件,没有内容,文件大小为0字节。
我的代码如下:
JSP
<td align="center"> <s:url action="downloadFile" var="urlTag" escapeAmp="false"><s:param name="work_id"><s:property value="%{#request.workid}"/></s:param> </s:url>
<a href="<s:property value="#urlTag" />">Download</a><br> </td>
struts.xml中
<action name="downloadFile" class="com.sales.action.DownloadFileAction">
<result name="success" type="stream">
<param name="contentType">application/octet-stream</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">attachment;filename="${fileName}"</param>
<param name="bufferSize">4096</param>
</result>
</action>
Action.java 公共类DownloadFileAction扩展了ActionSupport {
private InputStream inputStream;
private String fileName;
private int work_id;
private long contentLength;
public DownloadFileAction() {
}
public int getWork_id() {
return work_id;
}
public void setWork_id(int work_id) {
this.work_id = work_id;
}
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
public void setContentLength(long contentLength) {
this.contentLength = contentLength;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public long getContentLength() {
return contentLength;
}
public String getFileName() {
return fileName;
}
public InputStream getInputStream() {
return inputStream;
}
public String execute() throws Exception {
DevWorkInfoDAO dao = new DevWorkInfoDAO();
List tmp = new ArrayList();
tmp = dao.fileDownload(work_id);
fileName = tmp.get(0).toString();
Blob b = (Blob) tmp.get(1);
inputStream = b.getBinaryStream();
return SUCCESS;
}
}
DAO.java
String sql = "SELECT * FROM salesmanagementsystem.devorder_file_tbl where id_devorder = "+work_id;
System.out.println(sql);
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
DevOrder_File_Tbl dev = new DevOrder_File_Tbl();
dev.setId(rs.getInt("id"));
dev.setFile_name(rs.getString("name_file"));
dev.setFile(rs.getBlob("file"));
tmp.add(dev);
}
return tmp;
答案 0 :(得分:0)
在struts.xml中,别忘了编写contentLength。
<param name="contentLength">contentLength</param>