ki11in9 2017-09-15 08:23 采纳率: 0%
浏览 700

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 2017-09-15 08:29
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗