本帖最后由 a8336675 于 2015-9-26 11:30 编辑
- 从客户端将图片转换二进制数据,并发送到服务器端时,数据长度丢失(比原来小一些),但是图片能正常显示出来。
- 请问这是正常的吗?还是代码有问题?
- 代码如下:
复制代码
class Client implements Runnable {
@Override
public void run() {
try {
// 1, 使用JFileChooser打开系统文件选择框,选择一个文件,并获取该文件对象
JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
String checkResult = null;
if ((checkResult = checkFile(file)) != null) {
System.out.println(checkResult);
return;
}
// 2, 确认是否上传
System.out.println("File Name: " + file.getName());
System.out.println("File Path: " + file.getAbsolutePath());
System.out.println("File Size: " + file.length() + " B");
System.out.println("确定是否上传该文件?(yes/no)");
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
if (reader.readLine().trim().equalsIgnoreCase("yes")) {
reader.close();
// 3, 建立Socket对象,连接服务器
Socket client = new Socket("192.168.0.102", 10004);
BufferedOutputStream output = new BufferedOutputStream(client.getOutputStream());
// 4, 从文件中读取数据,并写入Socket输出流中,发送到服务器
FileInputStream fis = new FileInputStream(file);
int length = 0;
byte[] buf = new byte[1024];
while ((length = fis.read(buf)) != -1) {
output.write(buf, 0, length);
}
client.shutdownOutput();
fis.close();
// 5, 获得服务器返回信息
byte[] returnStr = new byte[1024];
length = client.getInputStream().read(returnStr);
System.out.println(new String(returnStr, 0, length));
client.close();
} else {
reader.close();
System.out.println("上传操作取消.");
}
}
} catch (Exception e) {
MyLog.printLog(e);
}
}
/**
* 检查文件规范性
* @param file
* @return
*/
public String checkFile(File file) {
String fileName = file.getName();
if (!fileName.endsWith(".jpg")) {
return "仅支持jpg图片上传.";
}
if (file.length() > 1024 * 1024 * 10) {
return "仅支持10M以下的图片上传.";
}
return null;
}
}
/**
* 用于上传图片的服务器端
*/
class Server implements Runnable {
@Override
public void run() {
try {
// 1, 建立ServerSocket对象,监听端口
ServerSocket server = new ServerSocket(10004);
while (true) {
// 2, 等待客户端连接
Socket client = server.accept();
new Thread(new uploadPicThread(client)).start();
}
} catch (Exception e) {
MyLog.printLog(e);
}
}
}
/**
* 用于接收图片数据,并封装成文件的线程类
*/
class uploadPicThread implements Runnable {
private Socket client;
public uploadPicThread(Socket client) {
this.client = client;
}
@Override
public void run() {
try {
String ip = client.getInetAddress().getHostAddress();
int port = client.getPort();
System.out.println("客户端(" + ip + ":" + port + ")连接成功.");
System.out.println("文件开始上传...");
// 3, 接受数据,封装成文件
FileOutputStream fos = new FileOutputStream("E:/ithima/dest/day23/" + ip + "-" + System.currentTimeMillis() / 1000 + ".jpg");
int length = 0;
int count = 0;
byte[] buf = new byte[1024];
while ((length = client.getInputStream().read(buf)) != -1) {
fos.write(buf, 0, length);
count += length;
}
System.out.println("上传完毕.文件大小: " + count + " B");
byte[] returnStr = "上传完毕.".getBytes();
client.getOutputStream().write(returnStr);
System.out.println("客户端(" + ip + ":" + port + ")断开连接.");
System.out.println();
fos.close();
client.close();
} catch (Exception e) {
MyLog.printLog(e);
}
}
}
|
|