黑马程序员技术交流社区
标题:
TCP上传图片问题,图片建立但为空,大神帮忙看下
[打印本页]
作者:
z13561291
时间:
2015-11-24 13:00
标题:
TCP上传图片问题,图片建立但为空,大神帮忙看下
<p>class PicClient</p><p>{</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>public static void main(String[] args) throws IOException</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>{</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>Socket s = new Socket("169.254.22.63",10003);</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>FileInputStream fis = new FileInputStream("F:\\线程生命周期.png");</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>OutputStream os = s.getOutputStream();</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>byte[] arr = new byte[1024];</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>int len = 0;</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>while((len=fis.read())!=-1){</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>os.write(arr,0,len);</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>}</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>//s.shutdownOutput();</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>InputStream is = s.getInputStream();</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>byte[] arr1 = new byte[1024];</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>int in = is.read(arr1);</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>System.out.println(new String(arr1,0,in));</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>is.close();</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>fis.close();</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>s.close();</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>}</p><p>}</p><p>class PicServer</p><p>{</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>public static void main(String[] args) throws IOException</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>{</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>ServerSocket ss = new ServerSocket(10003);</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>Socket s = ss.accept();</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>InputStream is = s.getInputStream();</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>FileOutputStream os = new FileOutputStream("pic.png");</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>byte[] arr = new byte[1024];</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>int len =0;</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>while((len=is.read())!=-1){</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>os.write(arr,0,len);</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>}</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>OutputStream os1 = s.getOutputStream();</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>os1.write("上传成功".getBytes());</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>os.close();</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>s.close();</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>ss.close();</p><p><span class="Apple-tab-span" style="white-space:pre"> </span>}</p><p>}</p>
复制代码
运行完后 客户端能看到服务器打的上传成功 也建立文件了,但文件为空 这是为什么?
麻烦帮忙看下
作者:
z13561291
时间:
2015-11-24 13:01
import java.io.*;
import java.net.*;
class PicClient
{
public static void main(String[] args) throws IOException
{
Socket s = new Socket("169.254.22.63",10003);
FileInputStream fis = new FileInputStream("F:\\线程生命周期.png");
OutputStream os = s.getOutputStream();
byte[] arr = new byte[1024];
int len = 0;
while((len=fis.read())!=-1){
os.write(arr,0,len);
}
//s.shutdownOutput();
InputStream is = s.getInputStream();
byte[] arr1 = new byte[1024];
int in = is.read(arr1);
System.out.println(new String(arr1,0,in));
is.close();
fis.close();
s.close();
}
}
class PicServer
{
public static void main(String[] args) throws IOException
{
ServerSocket ss = new ServerSocket(10003);
Socket s = ss.accept();
InputStream is = s.getInputStream();
FileOutputStream os = new FileOutputStream("pic.png");
byte[] arr = new byte[1024];
int len =0;
while((len=is.read())!=-1){
os.write(arr,0,len);
}
OutputStream os1 = s.getOutputStream();
os1.write("上传成功".getBytes());
os.close();
s.close();
ss.close();
}
}
作者:
hdhunter
时间:
2015-11-24 13:08
先确定当前操作系统,有没有权限写文件。
作者:
z13561291
时间:
2015-11-24 13:08
找到问题了。。。。while((len=fis.read())!=-1){
os.write(arr,0,len);
fis.read(arr)需要加数组 要不读完了 写不到数组去。。。
作者:
hdhunter
时间:
2015-11-24 13:15
如果文件很大,flush一下。
作者:
z13561291
时间:
2015-11-24 15:31
PrintWriter 写的 当带入参数true时 可以自动刷新的
作者:
HI耀玮
时间:
2015-11-24 18:24
进来看看,好牛逼
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2