- //客户端代码
- public class Demo1Client {
- public static void main(String[] args) throws IOException {
- Socket s = new Socket(InetAddress.getByName("127.0.0.1"), 10086);
- OutputStream os = s.getOutputStream();
- FileInputStream fis = new FileInputStream(
- "D:\\workspace\\day25_HomeWork\\aaa\\MrOCC.jpg");
- byte[] bys = new byte[1024];
- int len = 0;
- while ((len = fis.read(bys)) != -1) {
- os.write(bys, 0, len);
- }
- fis.close();
- s.close();
- }
- }
复制代码- //服务器端
- public class Demo1Server {
- public static void main(String[] args) throws IOException {
- ServerSocket ss = new ServerSocket(10086);
- Socket s = ss.accept();
- InputStream is = s.getInputStream();
- FileOutputStream fos = new FileOutputStream(
- "D:\\workspace\\day25_HomeWork\\bbb\\MrOCC.jpg");
- byte[] bys = new byte[1024];
- int len = is.read(bys);
- while ((len = is.read(bys)) != -1) {
- fos.write(bys, 0, len);
- }
- fos.close();
- s.close();
- ss.close();
- }
- }
复制代码 直接复制的代码,导包省了,我的思路就是首先图片要转成字节流,字节流上传到服务器端,服务器收到了将直接流写会文件
我现在的代码不成功,自己改了改后有时图片只有部分显示,这说明上传是没问题了,是不是读写出错了?
大家帮忙看看吧
|