无法打开从套接字接收的文件,安卓 Java。

huangapple go评论70阅读模式
英文:

Can't open file received from socket android java

问题

我正在制作一个使用套接字的应用程序,其中我想要在同一Wi-Fi网络中共享两台设备之间的数据。使用我的代码,我可以成功地在两台设备之间共享文件,并且文件的大小与特定名称一致地保存在设备存储中,但是当我尝试在文件管理器中打开它时,无法打开。我已经尝试了使用mp4文件和apk文件。

这是发送方的代码:

@Override
public void run() {
    //File file = new File(src);
    File file = new File(Environment.getExternalStorageDirectory() + "/c.mp4");

    byte[] bytes = new byte[(int) file.length()];

    BufferedInputStream bis;
    try {
        bis = new BufferedInputStream(new FileInputStream(file));
        DataInputStream dis = new DataInputStream(bis);
        OutputStream os = socket.getOutputStream();
        DataOutputStream dos = new DataOutputStream(os);
        dos.writeUTF("anything");
        dos.writeLong(bytes.length);
        int read;
        while ((read = dis.read(bytes)) != -1){
            dos.write(bytes,0,read);
        }

        socket.close();

        final String sentMsg = "文件已发送至:" + socket.getInetAddress();
        MainActivity.this.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                Toast.makeText(MainActivity.this, sentMsg, Toast.LENGTH_LONG).show();
            }});

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

这是接收方的代码:

@Override
public void run() {
    Socket socket = null;
    int bytesRead;
    InputStream in;
    int bufferSize=0;

    try {
        socket = new Socket(dstAddress, dstPort);

        bufferSize = socket.getReceiveBufferSize();
        in = socket.getInputStream();
        DataInputStream clientData = new DataInputStream(in);
        File file = new File(Environment.getExternalStorageDirectory(), "c.mp4");
        OutputStream output = new FileOutputStream(file);
        byte[] buffer = new byte[bufferSize];
        int read;
        while ((read = clientData.read(buffer)) != -1){
            output.write(buffer, 0 , read);
        }

        socket.close();

        MainActivity2.this.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                Toast.makeText(MainActivity2.this, "已完成", Toast.LENGTH_LONG).show();
            }});

    } catch (IOException e) {
        e.printStackTrace();

        final String eMsg = "出现错误:" + e.getMessage();
        MainActivity2.this.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                Toast.makeText(MainActivity2.this,
                        eMsg,
                        Toast.LENGTH_LONG).show();
            }});
    } finally {
        if(socket != null){
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
英文:

I am making an app with socket in which i want to share data from two devices in the same wifi. With my code i can share the file successfully between two devices with the exact size of the file and saves into the device storage with a specific name but when i try to open it fails to open in the file manager. I have tried this with mp4 file and apk files

Here is the sender's code

        @Override
public void run() {
//File file = new File(src);
File file = new File(Environment.getExternalStorageDirectory() + "/c.mp4");
byte[] bytes = new byte[(int) file.length()];
BufferedInputStream bis;
try {
bis = new BufferedInputStream(new FileInputStream(file));
DataInputStream dis = new DataInputStream(bis);
OutputStream os = socket.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
dos.writeUTF("anything");
dos.writeLong(bytes.length);
int read;
while ((read = dis.read(bytes)) != -1){
dos.write(bytes,0,read);
}
//os.write(bytes, 0, bytes.length);  //commented
//os.flush();   //commented
socket.close();
final String sentMsg = "File sent to: " + socket.getInetAddress();
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, sentMsg, Toast.LENGTH_LONG).show();
}});
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

And here is the receiver


@Override
public void run() {
Socket socket = null;
int bytesRead;
InputStream in; //changed
int bufferSize=0;
try {
socket = new Socket(dstAddress, dstPort);
bufferSize = socket.getReceiveBufferSize();
in = socket.getInputStream();
DataInputStream clientData = new DataInputStream(in);
File file = new File(Environment.getExternalStorageDirectory(), "c.mp4");
OutputStream output = new FileOutputStream(file);
byte[] buffer = new byte[bufferSize];
int read;
while ((read = clientData.read(buffer)) != -1){
output.write(buffer, 0 , read);
}
//bos.close();  //commented
socket.close();
MainActivity2.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity2.this, "Finished", Toast.LENGTH_LONG).show();
}});
} catch (IOException e) {
e.printStackTrace();
final String eMsg = "Something wrong: " + e.getMessage();
MainActivity2.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity2.this,
eMsg,
Toast.LENGTH_LONG).show();
}});
} finally {
if(socket != null){
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

答案1

得分: 0

你需要以与将其写入套接字的输出流相同的方式来读取客户端数据(DataInputStream)。

utf-8字符串,
长整型值,
字节序列

您的mp4文件内容将以"anything"和长度为前缀,因此它将被视为损坏的文件。

保存文件时不包括("anything"和长度)。

英文:

You need to read the clientData (DataInputStream) in the same way you write it to the output stream of the socket.

uft-8 string,
long value,
seq. of bytes

Your mp4 file content will prefixed with "anything" and length. so it will be considered as corrupted file.

save the file without ("anything" and length)

huangapple
  • 本文由 发表于 2020年8月5日 14:34:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63259670.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定