gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:21:17 26 4
gpt4 key购买 nike

首先,我上传了一个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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com