Access (.mdb) 文件在 servlet 写入客户端期间损坏 [英] Access (.mdb) file corrupted during servlet write to the client

查看:70
本文介绍了Access (.mdb) 文件在 servlet 写入客户端期间损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这原本是一个不同线程的第 2 部分,但另一个用途建议我将第 2 部分分成它自己的主题,所以我们开始吧.原始线程在这里(原始线程)

This was originally a part 2 of a different thread, but another use suggested that I separate the part 2 into it's own topic, so here we go. Original thread is here (Original Thread)

我正在使用 Jackcess 创建一个 V2010 mdb 文件,我需要将该文件传输到将使用 Access 2013 打开它的客户端.Jackcess 本身可以工作 - V2010 创建一个文件,当文件通过第三方软件(例如 FAR)通过 FTP 传输到客户端时,Access 2013 可以打开该文件.但是,当我尝试通过 servlet 将此文件上传到客户端时(这是该项目的目标),客户端上的 Access 显示无法识别的数据库格式...文件名...".这是使用的代码用于上传.代码本身有效,文件已传输,如果保存,则大小为非零 - 但 Access 无法打开它.

I am using Jackcess to create a V2010 mdb file that I need to transfer to a client that will use Access 2013 to open it. Jackcess itself works - V2010 creates a file that Access 2013 can open when the file is FTP'd to the client by a third party software, such as FAR. However, when I try to upload this file to the client through the servlet (as is the goal of this project), Access on the client says "Unrecognized database format "...file name...". This is the code used for upload. Code itself works, file is transferred, has a non-zero size if it's saved - but Access cannot open it.

注意,对于内容类型,我还尝试了 vnd.msassess 和 octed-stream,但结果相同.此外,我尝试关闭数据库并从文件名创建 FileInputStream,并且如示例中所示,尝试通过调用 mydb.getFile() 创建 FileInputStream.没有区别.

Note, for content type I also tried vnd.msassess and octed-stream, with same unsuccessful results. Also, I tried closing the db and creating the FileInputStream from the file name, and, as in the example, tried to create FileInputStream by calling mydb.getFile(). No difference.

response.setContentType("application/vnd.ms-access");
String fileName = "SomeFileName.mdb"; 
response.setHeader("Content-Disposition", "attachment; filename="+fileName);
Database mydb = generateMDBFile();
FileInputStream fis = new FileInputStream(mydb.getFile());
OutputStream os = response.getOutputStream();
byte[] buffer = new byte[1024];
try {
     int byteRead = 0;
     while ((byteRead = fis.read()) != -1) {
           os.write(buffer, 0, byteRead);
     }
     os.flush();
 } catch (Exception excp) {
     excp.printStackTrace();
 } finally {
     os.close();
     fis.close();
 }

为什么这段代码会破坏 mdb 文件?每次都会发生这种情况,无论大小(我尝试了一个 2 列/1 行的小文件,以及一个 40 列和 80000 行的大文件)

Why does this code corrupt the mdb file? This happens every time, regardless of the size (I tried a tiny 2 column/1 row file, and a huge file with 40 columns and 80000 rows)

谢谢!

推荐答案

您忘记填充缓冲区.使用

You forgot to fill the buffer. Use

// ...
while ((byteRead = fis.read(buffer)) != -1) {
       os.write(buffer, 0, byteRead);
 }
// ...

这篇关于Access (.mdb) 文件在 servlet 写入客户端期间损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆