如何使用SpringMVC框架从MySQL下载BLOB

如何使用SpringMVC框架从MySQL下载BLOB,mysql,hibernate,spring-mvc,blob,Mysql,Hibernate,Spring Mvc,Blob,我已经创建了将pdf文件上传到MySQL数据库BLOB中的代码 HTML代码: <form method="post" action="doUpload" enctype="multipart/form-data"> <table border="0"> <tr> <td>Pick file #1:</td> <td><input type="fi

我已经创建了将pdf文件上传到MySQL数据库BLOB中的代码

HTML代码:

<form method="post" action="doUpload" enctype="multipart/form-data">
    <table border="0">
        <tr>
            <td>Pick file #1:</td>
            <td><input type="file" name="fileUpload" size="50" /></td>
        </tr>
        <tr>
             <td colspan="2" align="center"><input type="submit" value="Upload" /></td>
        </tr>
     </table>
</form>

我可以将PDF文件上传到MySQL表的blob字段中。但我不知道如何检索blob数据,作为超链接,在那里我可以单击链接并下载pdf文件。请帮帮我。

您可以尝试从MySQL获取文档内容,然后在
响应
对象中设置数据流

使用
response.setHeader()
方法设置
“内容处置”
将在浏览器中启动另存为对话框,供用户下载文件

@RequestMapping("/retrieve/{fileName}")
public String download(@PathVariable("fileName")
        String fileName, HttpServletResponse response) {

    DownloadFile downloadDocument = downloadFileDao.get(fileName);
    try {
        response.setHeader("Content-Disposition", "inline; filename=\"" + fileName + "\"");
        OutputStream out = response.getOutputStream();
        response.setContentType(downloadDocument.getContentType());
        IOUtils.copy(downloadDocument.getContent().getBinaryStream(), out);
        out.flush();
        out.close();

    } catch (SQLException e) {
        System.out.println(e.toString());
        //Handle exception here
    } catch (IOException e) {
        System.out.println(e.toString());
        //Handle exception here
    }

    return "Success";
}
@RequestMapping("/retrieve/{fileName}")
public String download(@PathVariable("fileName")
        String fileName, HttpServletResponse response) {

    DownloadFile downloadDocument = downloadFileDao.get(fileName);
    try {
        response.setHeader("Content-Disposition", "inline; filename=\"" + fileName + "\"");
        OutputStream out = response.getOutputStream();
        response.setContentType(downloadDocument.getContentType());
        IOUtils.copy(downloadDocument.getContent().getBinaryStream(), out);
        out.flush();
        out.close();

    } catch (SQLException e) {
        System.out.println(e.toString());
        //Handle exception here
    } catch (IOException e) {
        System.out.println(e.toString());
        //Handle exception here
    }

    return "Success";
}