关于对象流读不进来的问题 在线跪求大神
Socket socket = null;/**
* 从连接中读取传入信息
*/
public InputEntity getTnputEntity(Socket socket) {
this.socket = socket;
ObjectInputStream objectInputStream = null;
InputEntity inputEntity = new InputEntity();
try {
objectInputStream = new ObjectInputStream(socket.getInputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("获取连接异常");
e.printStackTrace();
}
try {
Object object = objectInputStream.readObject();
System.out.println(object);
inputEntity = (InputEntity) object;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return inputEntity;
下面的是客户端
ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
InputEntity inputEntity = new InputEntity();
inputEntity.setHandle("F_id");
inputEntity.setId(123);
objectOutputStream.writeObject(inputEntity);
objectOutputStream.flush();
求大神解答啊 --------------------编程问答-------------------- java.lang.ClassNotFoundException: com.chenkai.test.InputEntity
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:247)
at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:604)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1575)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1496)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1732)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
at com.forward.five.jokebll.JokeHandle.getTnputEntity(JokeHandle.java:53)
at com.forward.five.jokebll.JokeSocket.run(JokeSocket.java:44)
Exception in thread "Thread-1" java.lang.NullPointerException
at com.forward.five.jokebll.JokeHandle.dataHandle(JokeHandle.java:91)
at com.forward.five.jokebll.JokeSocket.run(JokeSocket.java:45)
Exception in thread "main" java.lang.NullPointerException
at com.forward.five.jokebll.StartTest.main(StartTest.java:40)
这是报错 --------------------编程问答--------------------
--------------------编程问答-------------------- --------------------编程问答-------------------- Object object = objectInputStream.readObject();
System.out.println(object);
inputEntity = (InputEntity) object;
这种问题调试一下就可以确认!红色部分的代码得到的object没有判断是否为空就直接强转成InputEntity对象。
还有你用把代码写全了格式化一下,吧socket获取连接对象那里的代码贴出来。 --------------------编程问答-------------------- public void run() {
while (flag) {
JokeHandle jokeHandle = new JokeHandle();
try {
socket = serverSocket.accept();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("获取连接错误");
e.printStackTrace();
}
InputEntity inputEntity = jokeHandle.getTnputEntity(socket);
jokeHandle.dataHandle(inputEntity);
if (jokeHandle.fileOuputEntity != null) {
jokeHandle.sendEntity(jokeHandle.fileOuputEntity);
} else if (jokeHandle.userOutputEntity != null) {
jokeHandle.sendEntity(jokeHandle.userOutputEntity);
} else if (jokeHandle.flist != null) {
jokeHandle.sendEntity(jokeHandle.flist);
} else {
System.out.println("执行错误!!");
}
}
public class JokeHandle {
/**
*
*/
// 声明返回的实体类对象
FileOuputEntity fileOuputEntity = null;
UserOutputEntity userOutputEntity = null;
List<FileOuputEntity> flist = null;
Jokebll jokebll = new Jokebll();
Socket socket = null;
/**
* 从连接中读取传入信息
*/
public InputEntity getTnputEntity(Socket socket) {
this.socket = socket;
ObjectInputStream objectInputStream = null;
InputEntity inputEntity = new InputEntity();
try {
objectInputStream = new ObjectInputStream(new BufferedInputStream(
socket.getInputStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("获取连接异常");
e.printStackTrace();
}
Object object = null;
try {
object = objectInputStream.readObject();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println(object);
if (object != null) {
inputEntity = (InputEntity) object;
try {
objectInputStream.close();
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return inputEntity;
}
/**
* 数据操作
*/
@SuppressWarnings("null")
public void dataHandle(InputEntity inputEntity) {
String handle = inputEntity.getHandle();
String type = inputEntity.getType();
String email = inputEntity.getEmail();
String name = inputEntity.getName();
String password = inputEntity.getPassword();
long tel = inputEntity.getTel();
long id = inputEntity.getId();
long uid = inputEntity.getUid();
String review = inputEntity.getReview();
if (handle.equals("F_type")) {
FileOuputEntity fileOnputEntity = null;
flist = new ArrayList<FileOuputEntity>();
List<FilesEntity> filesEntities = jokebll.fTypeSelect(type);
for (int i = 0; i < filesEntities.size(); i++) {
FilesEntity filesEntity = new FilesEntity();
filesEntity = filesEntities.get(i);
BufferedImage image = null;
BufferedImage bufferedImage = null;
try {
image = ImageIO.read(new File(filesEntity.getUrl()));
bufferedImage = smallimage(image);
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("打开图片文件失败");
e.printStackTrace();
}
byte[] imageBytes = null;
byte[] bufferimagebytes = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ByteArrayOutputStream bufferbaos = new ByteArrayOutputStream();
try {
ImageIO.write(image, "jpg", baos);
ImageIO.write(bufferedImage, "jpg", bufferbaos);
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("图片生成流失败");
e.printStackTrace();
}
imageBytes = baos.toByteArray();
bufferimagebytes = bufferbaos.toByteArray();
List<Review> lrList = jokebll.selectFileOfId(null, 0,
filesEntity.getId());
fileOnputEntity.setId(filesEntity.getId());
fileOnputEntity.setExplains(filesEntity.getExplains());
fileOnputEntity.setFavorateNum(filesEntity.getFavoritenum());
fileOnputEntity.setImage(imageBytes);
fileOnputEntity.setBimage(bufferimagebytes);
fileOnputEntity.setReviews(lrList);
flist.add(fileOnputEntity);
}
} else if (handle.equals("F_id")) {
FilesEntity filesEntity = null;
List<Object> olistList = jokebll.fidSelect(id);
for (int i = 0; i < olistList.size(); i++) {
if (i == 0) {
filesEntity = (FilesEntity) olistList.get(i);
BufferedImage image = null;
BufferedImage bufferedImage = null;
try {
image = ImageIO.read(new File(filesEntity.getUrl()));
bufferedImage = smallimage(image);
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("打开图片文件失败");
e.printStackTrace();
}
byte[] imageBytes = null;
byte[] bufferimagebytes = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ByteArrayOutputStream bufferbaos = new ByteArrayOutputStream();
try {
ImageIO.write(image, "jpg", baos);
ImageIO.write(bufferedImage, "jpg", bufferbaos);
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("图片生成流失败");
e.printStackTrace();
}
imageBytes = baos.toByteArray();
bufferimagebytes = bufferbaos.toByteArray();
List<Review> lrList = jokebll.selectFileOfId(null, 0,
filesEntity.getId());
fileOuputEntity.setId(filesEntity.getId());
fileOuputEntity.setExplains(filesEntity.getExplains());
fileOuputEntity
.setFavorateNum(filesEntity.getFavoritenum());
fileOuputEntity.setImage(imageBytes);
fileOuputEntity.setBimage(bufferimagebytes);
fileOuputEntity.setReviews(lrList);
}
}
} else if (handle.equals("U_login")) {
UserEntity userEntity = new UserEntity();
userEntity = jokebll.uLogin(name, password);
userOutputEntity.setId(userEntity.getId());
userOutputEntity.setName(userEntity.getName());
} else if (handle.equals("U_sign")) {
UserEntity userEntity = new UserEntity();
userEntity.setName(name);
userEntity.setPassword(password);
userEntity.setEmail(email);
userEntity.setTel(String.valueOf(tel));
jokebll.uSign(userEntity);
}
else if (handle.equals("U_version")) {
}
else if (handle.equals("U_review")) {
List<Review> reviews = jokebll.selectFileOfId(review, id, uid);
fileOuputEntity.setReviews(reviews);
}
}
/**
* 生成缩略图
*
* @param bufferedImage
* @return
*/
private BufferedImage smallimage(BufferedImage bufferedImage) {
BufferedImage image = bufferedImage;
AffineTransform transform = new AffineTransform();
transform.setToScale((double) 320 / image.getWidth(), (double) 400
/ image.getWidth());
AffineTransformOp ato = new AffineTransformOp(transform, null);
BufferedImage newimage = new BufferedImage(400, 400 * image.getHeight()
/ image.getWidth(), BufferedImage.TYPE_3BYTE_BGR);
ato.filter(image, newimage);
return newimage;
}
/**
* 返回数据方法
*
* @return
*/
public boolean sendEntity(FileOuputEntity fileOuputEntitys) {
ObjectOutputStream objectOutputStream = null;
try {
objectOutputStream = new ObjectOutputStream(socket
.getOutputStream());
objectOutputStream.writeObject(fileOuputEntitys);
objectOutputStream.flush();
objectOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
public boolean sendEntity(UserOutputEntity userOutputEntitys) {
ObjectOutputStream objectOutputStream = null;
try {
objectOutputStream = new ObjectOutputStream(socket
.getOutputStream());
objectOutputStream.writeObject(userOutputEntitys);
objectOutputStream.flush();
objectOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
public boolean sendEntity(List<FileOuputEntity> flists) {
ObjectOutputStream objectOutputStream = null;
try {
objectOutputStream = new ObjectOutputStream(socket
.getOutputStream());
objectOutputStream.writeObject(flists);
objectOutputStream.flush();
objectOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
}
终于有大神回复了 服务器的大体就是这样了
补充:Java , Java SE