Java 无法传输图像文件
您好,这是我的客户端程序,用于传输图像,当传输图像文件时,它已损坏,无法打开该图像文件,我无法识别错误,是否有人可以帮助我Java 无法传输图像文件,java,Java,您好,这是我的客户端程序,用于传输图像,当传输图像文件时,它已损坏,无法打开该图像文件,我无法识别错误,是否有人可以帮助我 DataInputStream input = new DataInputStream(s.getInputStream()); DataOutputStream output = new DataOutputStream(s.getOutputStream()); System.out.println("Writing......."); FileInputStre
DataInputStream input = new DataInputStream(s.getInputStream());
DataOutputStream output = new DataOutputStream(s.getOutputStream());
System.out.println("Writing.......");
FileInputStream fstream = new FileInputStream("Blue hills.jpg");
DataInputStream in = new DataInputStream(fstream);
byte[] buffer = new byte[1000];
int bytes = 0;
while ((bytes = fstream.read(buffer)) != -1) {
output.write(buffer, 0, bytes);
}
in.close();
我假设s是一个套接字,您正试图通过网络传输文件?下面是一个使用套接字发送文件的示例。它只是在线程中设置一个服务器套接字并连接到自身
public static void main(String[] args) throws IOException {
new Thread() {
public void run() {
try {
ServerSocket ss = new ServerSocket(3434);
Socket socket = ss.accept();
InputStream in = socket.getInputStream();
FileOutputStream out = new FileOutputStream("out.jpg");
copy(in, out);
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
Socket socket = new Socket("localhost", 3434);
OutputStream out = socket.getOutputStream();
FileInputStream in = new FileInputStream("in.jpg");
copy(in, out);
out.close();
in.close();
}
public static void copy(InputStream in, OutputStream out) throws IOException {
byte[] buf = new byte[8192];
int len = 0;
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
}
}
本地还是在计算机之间?我验证了它在本地对我有效。