mysql - 文件大小为 0 字节(使用 Struts2 和 MySQL 下载文件应用程序)

标签 mysql download struts2

首先,我上传了一个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 公共(public)类 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;

最佳答案

在struts.xml中,不要忘记写contentLength。

<param name="contentLength">contentLength</param>

关于mysql - 文件大小为 0 字节(使用 Struts2 和 MySQL 下载文件应用程序),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36563902/

相关文章:

mysql - 是否可以使用不同的排序规则(每行)或在 MySQL 中添加您自己的排序规则?

mysql 返回固定数量的查询

使用 Ajax 下载 JQuery 文件

dojo - 我无法在 javascript 中访问 dojo datetimepicker 标记的值

java - 在 Struts 2 中为特定操作映射配置 SiteMesh?

mysql - 不同表中多个值的总和

c# - MySQLX.GetSession异常: : 'Expected message id: 2. Received message id: 10'

ios - 如何在 objective-c 中从谷歌驱动器下载jpg、png、pdf、doc.ppt、rtf等文件

php - 如何在 IE (PHP) 中下载服务器文件的更新版本?

java - struts2中的文件上传进度条?