import java.io.*;
public class TCPServer
{
public static void main(String args[]){
uploadFile();
}
//上传图片的服务器
public static void uploadFile(){
ServerSocket ss = null;
Socket socket = null;
try{
ss = new ServerSocket(9999);
while(true){
socket = ss.accept();
new Thread(new UploadFileServer(socket)).start();
}
}catch(Exception e){
}finally{
try{
if(ss!=null) ss.close();
}catch(Exception e){
}
}
}
//上传文本
public static void uploadTxt(){
ServerSocket ss = null;
Socket socket = null;
BufferedWriter bw = null;
try{
ss = new ServerSocket(9999);
socket = ss.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
bw = new BufferedWriter(new FileWriter("q:/2.txt"));
PrintWriter pw = new PrintWriter(socket.getOutputStream(),true);
String line = null;
while((line=br.readLine())!=null){
bw.write(line);
bw.newLine();
bw.flush();
}
System.out.println("上次完成");
pw.println("上次完成");
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(bw!=null) bw.close();
if(socket!=null) socket.close();
if(ss!=null) ss.close();
}catch(Exception e){
}
}
}
public static void converterDemo(){
ServerSocket ss = null;
Socket socket = null;
try{
ss = new ServerSocket(9999);
socket = ss.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter pw = new PrintWriter(socket.getOutputStream(),true);
String input = null;
while(true){
input = br.readLine();
if("886".equals(input)){
break;
}
pw.println(input.toUpperCase());
}
}catch(Exception e){
}finally{
try{
if(socket!=null) socket.close();
if(ss!=null) ss.close();
}catch(Exception e){
}
}
}
public static void TCPServer1(){
ServerSocket server = null;
Socket socket = null;
try{
server = new ServerSocket(9999);
socket = server.accept();
InputStream in = socket.getInputStream();
byte[] buf = new byte[1024];
in.read(buf);
System.out.println(new String(buf));
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(socket!=null) socket.close();
if(server!=null) server.close();
}catch(Exception e){
}
}
}
}
class UploadFileServer implements Runnable
{
private Socket socket;
public UploadFileServer(Socket socket){
this.socket = socket;
}
public void run(){
FileOutputStream fos = null;
try{
String remoteIP = socket.getInetAddress().getHostAddress();
File dir = new File("q:/upload");
if(!dir.exists()||!dir.isDirectory()){
dir.mkdir();
}
File file = new File(dir,remoteIP+".jpg");
int count = 0;
while(file.exists()){
file = new File(dir,remoteIP+"("+(count++)+").jpg");
}
fos = new FileOutputStream(file);
PrintWriter pw = new PrintWriter(socket.getOutputStream(),true);
InputStream in = socket.getInputStream();
byte[] buf = new byte[1024];
int len = 0;
while((len=in.read(buf))!=-1){
fos.write(buf,0,len);
}
pw.println("上传完成");
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(fos!=null) fos.close();
if(socket!=null) socket.close();
}catch(Exception e){
}
}
}
}
——————————————————————————————————————————————————————————————————————
import java.net.*;
import java.io.*;
public class TCPClient
{
public static void main(String args[]){
uploadFile();
}
//上传图片
public static void uploadFile(){
Socket socket = null;
FileInputStream fis = null;
try{
socket = new Socket(InetAddress.getLocalHost(),9999);
fis = new FileInputStream("q:/魅族.jpg");
OutputStream out = socket.getOutputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
byte[] buf = new byte[1024];
int len = 0;
while((len=fis.read(buf))!=-1){
out.write(buf,0,len);
}
socket.shutdownOutput();
System.out.println(br.readLine());
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(socket!=null) socket.close();
if(fis!=null) fis.close();
}catch(Exception e){
}
}
}
public static void uploadTxt(){
Socket socket = null;
BufferedReader br = null;
try{
br = new BufferedReader(new FileReader("q:/1.txt"));
socket = new Socket(InetAddress.getLocalHost(),9999);
PrintWriter pw = new PrintWriter(socket.getOutputStream(),true);
BufferedReader br2 = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line = null;
while((line=br.readLine())!=null){
pw.println(line);
}
socket.shutdownOutput();
String backStr = br2.readLine();
System.out.println(backStr);
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(br!=null) br.close();
if(socket!=null) socket.close();
}catch(Exception e){
}
}
}
public static void converterDemo(){
Socket socket = null;
try{
socket = new Socket(InetAddress.getLocalHost(),9999);
PrintWriter pw = new PrintWriter(socket.getOutputStream(),true);
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedReader sysin = new BufferedReader(new InputStreamReader(System.in));
String line = null;
String backStr = null;
while(true){
line = sysin.readLine();
System.out.println("to:"+line);
pw.println(line);
if("886".equals(line)){
break;
}
backStr = br.readLine();
System.out.println("back:"+backStr);
}
}catch(Exception e){
}finally{
try{
if(socket!=null) socket.close();
}catch(Exception e){
}
}
}
public static void TCPClient1(){
Socket socket = null;
try{
socket = new Socket(InetAddress.getLocalHost(),9999);
OutputStream out = socket.getOutputStream();
out.write("你好".getBytes());
out.flush();
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(socket!=null) socket.close();
}catch(Exception e){
}
}
}
}