开发者

How to send a file from a MySQL BLOB as download to the enduser?

开发者 https://www.devze.com 2023-02-10 03:40 出处:网络
How can I send a file as download to the enduser? I have implemented a form submission of a page with a column of data as session of the DB. 开发者_Go百科This column includes one file which is saved a

How can I send a file as download to the enduser? I have implemented a form submission of a page with a column of data as session of the DB. 开发者_Go百科This column includes one file which is saved as BLOB in a MySQL database.

When handling actionforward and using the mapping file for a table with CV's, I can access the file content by cv.getFileContent(). Do I have to send the data byte by byte to the user? How do I offer the file as a download?


You should use servlet as download mechanism.

Submit your form to Servlet using Get or Post,then in Servlet Class implement doGet or doPost accordingly.

Using Hibernate fetch the column as byte Array.

protected void doGet(HttpServletRequest req, HttpServletResponse resp) {

    //Getting data s byte Array;
    byte[] csvFile = daoService.getFile(id);

    resp.setContentType("text/csv");
    resp.setContentLength((int) csvFile.length());
    ServletOutputStream out;

    try {
        out = resp.getOutputStream();
        out.write(csvFile);
        out.flush();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        out.close();
        out.flush();
    }

}

Hope it helps.

Update:

This is how I mapped the byte[].

<property
    name="image"
    update="true"
    insert="true"
    not-null="false"
    unique="false"
    type="binary"
>
    <column name="image" />
</property>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号