2 guze1266 guze1266 于 2017.09.15 16:23 提问

SQLite整个库文件用Java Soceket传输后不能使用了,报错见正文!求大佬帮助!!

需求是这样的,一个java程序(C/S的),然后用到SQLite,需要对整个库文件,也就是.db进行网络备份,于是使用Socket进行传输。结果再把备份文件传回来(也是Socket传),就用不了了,再连接数据库就报错:File opened that is not a database file (file is encrypted or is not a database)。求大佬分析分析,附上服务端客户端部分代码。
服务端:
接收的:
private static String receFile(Socket s, String cdkey) {
if (s != null) {
DataInputStream dis = null;
DataOutputStream dos = null;
String savePath = (ConfigControl.readData("backuppath") + cdkey + "/").replace('\', '/');
File tempfile = new File(savePath);
if (!tempfile.exists()) {
tempfile.mkdirs();
}
int bufferSize = 1024;
byte[] buf = new byte[bufferSize];

        try {
            dis = new DataInputStream(new BufferedInputStream(s.getInputStream()));
            savePath = savePath + dis.readUTF();
            dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(savePath)));

            int read = 0;
            while ((read = dis.read(buf)) > 0) {
                dos.write(buf, 0, read);
            }
            dos.flush();
            System.out.println("文件: " + savePath + "接收完成!");

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("接收文件失败!");
        }
        return savePath;
    } else {
        return "";
    }
    发送的:
    private static String[] sendFile(Socket s, String filePath) {
    String[] msg = null;
    if (s != null) {
        System.out.println("开始发送文件:" + filePath);
        File file = new File(filePath);
        int bufferSize = 1024;
        byte[] buf = new byte[bufferSize];
        try {
            DataInputStream fis = new DataInputStream(new BufferedInputStream(new FileInputStream(filePath)));
            DataOutputStream dos = new DataOutputStream(s.getOutputStream());
            dos.writeUTF("SYSTEM.db");
            dos.flush();
            dos.writeLong(file.length());
            dos.flush();

            int read = 0;
            while ((read = fis.read(buf)) > 0) {
                dos.write(buf, 0, read);
            }
            dos.flush();

            System.out.println("文件 " + filePath + "传输完成!");
            fis.close();
            msg = new String[] { "100311" };
            return msg;// 发送成功
        } catch (Exception e) {
            e.printStackTrace();
            msg = new String[] { "100301" };
            return msg;// 发送失败
        }
    } else {
        msg = new String[] { "0" };
        return msg;
    }
}

客户端:
接收的:
    /**
 * 接收文件
 * 
 * @param s
 * @return
 */
public String receFile() {
    if (s != null) {
        DataInputStream dis = null;
        DataOutputStream dos = null;
        String savePath = (ConfigControl.readData("path") + "/db/").replace('\\', '/');
        int bufferSize = 1024;
        byte[] buf = new byte[bufferSize];

        try {
            dis = new DataInputStream(new BufferedInputStream(s.getInputStream()));
            savePath = savePath + dis.readUTF();
            dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(savePath)));
            int read = 0;
            while ((read = dis.read(buf)) > 0) {
                dos.write(buf, 0, read);
            }
            dos.flush();
            System.out.println("文件: " + savePath + "接收完成!");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("接收文件失败!");
        }
        return "1";
    } else {
        return "0";
    }
}
发送的:
public String sendFile(String filePath) {
    if (s != null) {
        File file = new File(filePath);
        if (file.exists()) {
            int bufferSize = 1024;
            byte[] buf = new byte[bufferSize];
            try {
                DataInputStream fis = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));
                DataOutputStream dos = new DataOutputStream(s.getOutputStream());
                dos.writeUTF(GetData.gettime() + ".db");
                dos.flush();
                dos.writeLong(file.length());
                dos.flush();

                int read = 0;
                System.out.println("开始发送文件:" + filePath);
                while ((read = fis.read(buf)) > 0) {
                    dos.write(buf, 0, read);

                }
                dos.flush();
                s.shutdownOutput();

                System.out.println("文件 " + filePath + "传输完成!");
                fis.close();
                return "1";// 发送成功
            } catch (Exception e) {
                e.printStackTrace();
                return "100201";// 发送失败
            }
        } else {
            return "0";
        }
    } else {
        return "0";
    }
}

求大佬帮忙,刚接触Java不久,实在是没办法解决了!

1个回答

oyljerry
oyljerry   Ds   Rxr 2017.09.15 16:29

直接对比发送和接收后的文件的md5等,看是否相等,不等。就是读取或者通信中有数据丢失了

guze1266
guze1266 对比了,确实出问题了。但是我无法排除到底哪句代码导致的。大神可以帮我分析分析么?
2 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片