如何使用java套接字实现客户端和服务器之间的文件传输

如何使用java套接字实现客户端和服务器之间的文件传输,第1张

如何使用java套接字实现客户端和服务器之间的文件传输

快速阅读源代码,看来您距离并不远。以下链接应该有帮助(我对FTP做过类似的事情)。对于从服务器发送到客户端的文件,您首先需要一个文件实例和一个字节数组。然后,您将File读入字节数组,并将字节数组写入与客户端的InputStream对应的OutputStream。

http://www.rgagnon.com/javadetails/java-0542.html

编辑:这是一个工作正常的超简约文件发送器和接收器。确保您了解代码的两面都在做什么。

package filesendtest;import java.io.*;import java.net.*;class TCPServer {    private final static String fileToSend = "C:\test1.pdf";    public static void main(String args[]) {        while (true) { ServerSocket welcomeSocket = null; Socket connectionSocket = null; BufferedOutputStream outToClient = null; try {     welcomeSocket = new ServerSocket(3248);     connectionSocket = welcomeSocket.accept();     outToClient = new BufferedOutputStream(connectionSocket.getOutputStream()); } catch (IOException ex) {     // Do exception handling } if (outToClient != null) {     File myFile = new File( fileToSend );     byte[] mybytearray = new byte[(int) myFile.length()];     FileInputStream fis = null;     try {         fis = new FileInputStream(myFile);     } catch (FileNotFoundException ex) {         // Do exception handling     }     BufferedInputStream bis = new BufferedInputStream(fis);     try {         bis.read(mybytearray, 0, mybytearray.length);         outToClient.write(mybytearray, 0, mybytearray.length);         outToClient.flush();         outToClient.close();         connectionSocket.close();         // File sent, exit the main method         return;     } catch (IOException ex) {         // Do exception handling     } }        }    }}

package filesendtest;import java.io.*;import java.io.ByteArrayOutputStream;import java.net.*;class TCPClient {    private final static String serverIP = "127.0.0.1";    private final static int serverPort = 3248;    private final static String fileOutput = "C:\testout.pdf";    public static void main(String args[]) {        byte[] aByte = new byte[1];        int bytesRead;        Socket clientSocket = null;        InputStream is = null;        try { clientSocket = new Socket( serverIP , serverPort ); is = clientSocket.getInputStream();        } catch (IOException ex) { // Do exception handling        }        ByteArrayOutputStream baos = new ByteArrayOutputStream();        if (is != null) { FileOutputStream fos = null; BufferedOutputStream bos = null; try {     fos = new FileOutputStream( fileOutput );     bos = new BufferedOutputStream(fos);     bytesRead = is.read(aByte, 0, aByte.length);     do {  baos.write(aByte);  bytesRead = is.read(aByte);     } while (bytesRead != -1);     bos.write(baos.toByteArray());     bos.flush();     bos.close();     clientSocket.close(); } catch (IOException ex) {     // Do exception handling }        }    }}

有关

Java中未知长度的字节数组

编辑:以下可用于在传输前后对小文件进行指纹识别(如果需要,请使用SHA):

public static String md5String(File file) {    try {        InputStream fin = new FileInputStream(file);        java.security.MessageDigest md5er = MessageDigest.getInstance("MD5");        byte[] buffer = new byte[1024];        int read;        do { read = fin.read(buffer); if (read > 0) {     md5er.update(buffer, 0, read); }        } while (read != -1);        fin.close();        byte[] digest = md5er.digest();        if (digest == null) { return null;        }        String strDigest = "0x";        for (int i = 0; i < digest.length; i++) { strDigest += Integer.toString((digest[i] & 0xff)         + 0x100, 16).substring(1).toUpperCase();        }        return strDigest;    } catch (Exception e) {        return null;    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5615448.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存