黑马程序员技术交流社区
标题:
受不了了,看的我一个头两个大,求大神指点!
[打印本页]
作者:
薆情媬証書
时间:
2013-10-22 12:09
标题:
受不了了,看的我一个头两个大,求大神指点!
本帖最后由 薆情媬証書 于 2013-10-23 09:16 编辑
亲,帮忙看看,这段代码运行也没问题,可是执行后 ,却没有创建新文件,不知是哪里出了问题??
<p> import java.net.*;
import java.io.*;</p><p>class PicClient
{
public static void main(String[] args)throws Exception
{
Socket s = new Socket(InetAddress.getByName("192.168.1.100"),20000);
FileInputStream fis = new FileInputStream("d:\\1.jpg");</p><p> OutputStream os= s.getOutputStream();</p><p> byte[] buf = new byte[1024];
int len =0;
while ((len =fis.read(buf)) !=-1)
{
os.write(buf, 0 ,len);
}
s.shutdownOutput();</p><p> InputStream is = s.getInputStream();
len = is.read(buf);
System.out.println(new String(buf,0,len));
}
}</p><p>class PicServe
{
public static void main(String[] args) throws Exception
{
ServerSocket ss = new ServerSocket(20000);
while (true)
{
Socket s = ss.accept();</p><p> new Thread(new PicServeThread(s)).start();
}
}
}</p><p>
class PicServeThread implements Runnable
{
private Socket s;
PicServeThread(Socket s)
{
this.s=s;
}</p><p> public void run()
{
File file = new File(s.getInetAddress().getHostAddress()+".jpg");
try
{
if(!file.exists())
file.createNewFile();
InputStream is = s.getInputStream();</p><p> FileOutputStream fis = new FileOutputStream(file);</p><p> byte[] buf = new byte[1024];
int len = 0;
while ((len =is.read(buf))!=-1)
{
fis.write(buf,0,len);
}
System.out.println(s.getInetAddress().getHostAddress()+"上传了"+file.toString());
OutputStream os = s.getOutputStream();
os.write("上传成功!".getBytes());
}
catch (Exception e)
{
throw new RuntimeException("上传失败");
}
</p><p> }
}</p>
复制代码
求指点!!!
作者:
薆情媬証書
时间:
2013-10-22 12:11
该文件运行后正确的结果应该是有一个图片文件生成的!
作者:
yanglfree
时间:
2013-10-22 12:33
你D:\\下面有1.jpg这个文件吗?
作者:
杨增坤
时间:
2013-10-22 16:58
亲,要注意排版!
作者:
深情小建
时间:
2013-10-22 18:38
代码看过了,没有问题,
但是估计你几个地方的参数有问题
问题1:
你的ip是否正确192.168.1.100→192.168.0.100
问题2:
端口号也有可能被其他程序占用,换一个试试 例如:20000→20001
问题3:
源图片文件是否存在 d://1.jpg
如果上述问题都排除那么会在文件目录下生成192.168.0.100.jpg
代码粘帖如下:
import java.net.*;
import java.io.*;
class PicClient {
public static void main(String[] args) throws Exception {
Socket s = new Socket(InetAddress.getByName("192.168.0.100"), 20000);
FileInputStream fis = new FileInputStream("d:\\1.jpg");
OutputStream os = s.getOutputStream();
byte[] buf = new byte[1024];
int len = 0;
while ((len = fis.read(buf)) != -1) {
os.write(buf, 0, len);
}
s.shutdownOutput();
InputStream is = s.getInputStream();
len = is.read(buf);
System.out.println(new String(buf, 0, len));
}
}
class PicServe {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(20000);
while (true) {
Socket s = ss.accept();
new Thread(new PicServeThread(s)).start();
}
}
}
class PicServeThread implements Runnable {
private Socket s;
PicServeThread(Socket s) {
this.s = s;
}
public void run() {
File file = new File(s.getInetAddress().getHostAddress() + ".jpg");
try {
if (!file.exists())
file.createNewFile();
InputStream is = s.getInputStream();
FileOutputStream fis = new FileOutputStream(file);
byte[] buf = new byte[1024];
int len = 0;
while ((len = is.read(buf)) != -1) {
fis.write(buf, 0, len);
}
System.out.println(s.getInetAddress().getHostAddress() + "上传了"
+ file.toString());
OutputStream os = s.getOutputStream();
os.write("上传成功!".getBytes());
} catch (Exception e) {
throw new RuntimeException("上传失败");
}
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2