import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
class manffile2 {
public static void main(String[] args)throws Exception
{
ServerSocket serverstock=new ServerSocket(10009);
while(true){
Socket server=serverstock.accept();
new Thread( new PicThread(server)).start();
}
}
}
class PicThread implements Runnable
{
private Socket server;
PicThread(Socket server)
{
this.server=server;
}
public void run(){
try
{
InputStream inputstream=server.getInputStream();
FileOutputStream fileoutputstream=new FileOutputStream("2.txt");
int ch=0;
byte[] byt=new byte[1024];
while((ch= inputstream.read(byt))!=-1){
fileoutputstream.write(byt,0,ch);
}
OutputStream outputstream=server.getOutputStream();
byte[] byt2=new byte[1024];
outputstream.write("上传成功".getBytes());
server.close();
}
catch(Exception E){
}
}
}
----------------------------------------------------------------
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Stack;
class ManyFile {
public static void main(String[] args) throws Exception
{
Socket stock=new Socket("192.168.1.2",10009);
FileInputStream inputstream=new FileInputStream("1.txt");
OutputStream outputstream=stock.getOutputStream();
int ch=0;
byte[] byt=new byte[1024];
while((ch=inputstream.read(byt))!=-1){
outputstream.write(byt,0,ch);
}
stock.shutdownOutput();
InputStream intputstream=stock.getInputStream();
byte[] byt2=new byte[1024*1024];
int num=inputstream.read(byt2);
System.out.println(new String(byt2,0,num));
stock.close();
}
}
程序不知道哪里有问题报错:Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.checkBounds(String.java:398)
at java.lang.String.<init>(String.java:570)
at manyfile.ManyFile.main(ManyFile.java:37)
|
|