请教一个JAVA Socket问题。我想实现服务器向客户端发送一张图片。但是我这个程序不能实现。什么原因呢?
服务器端:packageTCP;importjava.io.BufferedInputStream;importjava.io.DataInputStream;impor...
服务器端:
package TCP;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Transfer2 {
int port = 8080;
void start() {
Socket s = null;
try {
ServerSocket ss = new ServerSocket(port);
while (true) {
// 选择进行传输的文件
String filePath = "F:\\我的军训照片\\sg.jpg";
File fi = new File(filePath);
System.out.println("文件长度:" + (int) fi.length());
// public Socket accept() throws
// IOException侦听并接受到此套接字的连接。此方法在进行连接之前一直阻塞。
s = ss.accept();
System.out.println("建立socket链接");
ObjectOutputStream ps = new ObjectOutputStream(s.getOutputStream());
ps.writeObject(fi);
ps.flush();
// 注意关闭socket链接哦,不然客户端会等待server的数据过来,
// 直到socket超时,导致数据不完整。
ps.close();
s.close();
System.out.println("文件传输完成");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String arg[]) {
new Transfer2().start();
}
}
客户端:
package TCP;
import java.net.*;
import java.io.*;
public class Transfer2 implements Runnable {
Socket clientsocket = null;
public static void main(String args[]) {
Thread thread = new Thread(new Transfer3());
thread.start();
}
public void run() {
try {
clientsocket = new Socket("localhost",8080);
System.out.println("连接已建立完毕!");
while(true)
{
InputStream is = clientsocket.getInputStream();
ObjectInputStream inObject = new ObjectInputStream(is);
File file = (File) inObject.readObject();
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream("D:\\sg.jpg");
byte[] buffer = new byte[1];
while (fis.read() != -1)
{
fos.write(buffer);
}
fos.flush();//可有可无
fis.close();
fos.close();
}
} catch (Exception e) {
System.out.println(e);
}
}
} 展开
package TCP;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Transfer2 {
int port = 8080;
void start() {
Socket s = null;
try {
ServerSocket ss = new ServerSocket(port);
while (true) {
// 选择进行传输的文件
String filePath = "F:\\我的军训照片\\sg.jpg";
File fi = new File(filePath);
System.out.println("文件长度:" + (int) fi.length());
// public Socket accept() throws
// IOException侦听并接受到此套接字的连接。此方法在进行连接之前一直阻塞。
s = ss.accept();
System.out.println("建立socket链接");
ObjectOutputStream ps = new ObjectOutputStream(s.getOutputStream());
ps.writeObject(fi);
ps.flush();
// 注意关闭socket链接哦,不然客户端会等待server的数据过来,
// 直到socket超时,导致数据不完整。
ps.close();
s.close();
System.out.println("文件传输完成");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String arg[]) {
new Transfer2().start();
}
}
客户端:
package TCP;
import java.net.*;
import java.io.*;
public class Transfer2 implements Runnable {
Socket clientsocket = null;
public static void main(String args[]) {
Thread thread = new Thread(new Transfer3());
thread.start();
}
public void run() {
try {
clientsocket = new Socket("localhost",8080);
System.out.println("连接已建立完毕!");
while(true)
{
InputStream is = clientsocket.getInputStream();
ObjectInputStream inObject = new ObjectInputStream(is);
File file = (File) inObject.readObject();
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream("D:\\sg.jpg");
byte[] buffer = new byte[1];
while (fis.read() != -1)
{
fos.write(buffer);
}
fos.flush();//可有可无
fis.close();
fos.close();
}
} catch (Exception e) {
System.out.println(e);
}
}
} 展开
展开全部
呵呵,这是第二次给你回答问题了。帮你把代码重构了,你自己体会下吧。你对线程,流的概念理解有点乱。
服务端:
package tcp;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Transfer_Server extends Thread {
int port = 8080;
public void run() {
Socket s = null;
try {
ServerSocket ss = new ServerSocket(port);
while (true) {
s = ss.accept();
String filePath = "D:\\1.JPG";
File fi = new File(filePath);
System.out.println("文件长度:" + (int) fi.length());
System.out.println("建立socket链接");
ObjectOutputStream ps = new ObjectOutputStream(s
.getOutputStream());
ps.writeObject(fi);
ps.flush();
ps.close();
s.close();
System.out.println("文件传输完成");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String arg[]) {
new Transfer_Server().start();
}
}
客户端:
package tcp;
import java.net.*;
import java.io.*;
public class Transfer_Client implements Runnable {
Socket clientsocket = null;
public static void main(String args[]) {
Thread thread = new Thread(new Transfer_Client());
thread.start();
}
public void run() {
try {
clientsocket = new Socket("localhost", 8080);
System.out.println("连接已建立完毕!");
//while (true) {此处代码不能这么写。你对Socket客户端的理解不太妥。
InputStream is = clientsocket.getInputStream();
ObjectInputStream inObject = new ObjectInputStream(is);
File file = (File) inObject.readObject();
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream("D:\\sg.jpg");
byte[] buffer = new byte[1];
while (fis.read(buffer) != -1) {
fos.write(buffer);
}
fos.flush();// 可有可无
fis.close();
fos.close();
//}
} catch (Exception e) {
System.out.println(e);
}
}
}
服务端:
package tcp;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Transfer_Server extends Thread {
int port = 8080;
public void run() {
Socket s = null;
try {
ServerSocket ss = new ServerSocket(port);
while (true) {
s = ss.accept();
String filePath = "D:\\1.JPG";
File fi = new File(filePath);
System.out.println("文件长度:" + (int) fi.length());
System.out.println("建立socket链接");
ObjectOutputStream ps = new ObjectOutputStream(s
.getOutputStream());
ps.writeObject(fi);
ps.flush();
ps.close();
s.close();
System.out.println("文件传输完成");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String arg[]) {
new Transfer_Server().start();
}
}
客户端:
package tcp;
import java.net.*;
import java.io.*;
public class Transfer_Client implements Runnable {
Socket clientsocket = null;
public static void main(String args[]) {
Thread thread = new Thread(new Transfer_Client());
thread.start();
}
public void run() {
try {
clientsocket = new Socket("localhost", 8080);
System.out.println("连接已建立完毕!");
//while (true) {此处代码不能这么写。你对Socket客户端的理解不太妥。
InputStream is = clientsocket.getInputStream();
ObjectInputStream inObject = new ObjectInputStream(is);
File file = (File) inObject.readObject();
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream("D:\\sg.jpg");
byte[] buffer = new byte[1];
while (fis.read(buffer) != -1) {
fos.write(buffer);
}
fos.flush();// 可有可无
fis.close();
fos.close();
//}
} catch (Exception e) {
System.out.println(e);
}
}
}
追问
非常感谢你帮了我两次忙!不胜感激啊!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询