客戶端需求:把一個圖片文件發送到服務端並讀取回饋信息。要求判斷文件是否存在及格式是否爲jpg或gif並要求文件小於2M。
服務端需求:接收客戶端發送過來的圖片數據。進行存儲後,回饋一個 上傳成功字樣。支持多用戶的併發訪問。
//////////////////////客戶端///////////////////////////public class UploadPicClient {
public static void main(String[] args) {
JFileChooser jfc=new JFileChooser();//彈出文件選擇框
int sel=jfc.showOpenDialog(null);//獲取選擇
if(sel==JFileChooser.APPROVE_OPTION){
File file=jfc.getSelectedFile();//得到選擇的文件
if(file.isDirectory()){//判斷文件是否是文件夾,一般是不會出現這個的,這裏只是簡單地寫一下
JOptionPane.showMessageDialog(null, "文件選擇錯誤,只能上傳文件!");
return;
}
if(!(file.getName().endsWith(".jpg")||file.getName().endsWith("gif"))){//用endsWith判斷其後綴名
JOptionPane.showMessageDialog(null, "選擇文件格式不正確");
return;
}
if(file.length()>2*1024*1024){//判斷文件的額大小是否符合要求
JOptionPane.showMessageDialog(null, "選擇文件大小已超過2M不可上傳");
return;
}
//滿足上傳的條件
try {
//Client聲明Socket
Socket s=new Socket(InetAddress.getByName("127.0.0.1"),9999);
//獲得名字InetAddress.getByName("127.0.0.1")
BufferedInputStream bis=new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream bout=new BufferedOutputStream(s.getOutputStream());
byte buf[]=new byte[2*1024*1024];//因爲限制了文件的額大小所以這裏就直接用2M
int len=0;
while((len=bis.read(buf))!=-1){
bout.write(buf,0,len);
bout.flush();
}
s.shutdownOutput();//停止發送標誌
InputStream in=s.getInputStream();//獲得反饋信息
byte buf2[]=new byte[64];
int len2= in.read(buf2);
String strecho=new String(buf2,0,len2);
System.out.println(strecho);
bis.close();//一定要關流
s.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//////////////////////服務器///////////////////////////
public class UploadServer {
public static void main(String[] args) {
try {
// 聲明ServerSocket 來等待握手
ServerSocket server = new ServerSocket(9999);
//在這裏main用來接收每接收一次就開一個線程,因爲題目的要求就是支持多用戶運行
while (true) {Socket s = server.accept();
new Thread(new Receive(s)).start();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class Receive implements Runnable {
private Socket s = null;
public Receive(Socket s) {//寫構造函數
this.s = s;
}
@Override
public void run() {
String ip = s.getInetAddress().getHostAddress();//獲得ip
// 源:s.getInputStream()
// 目的:FileOutputStream ---文件名設計: IP(1).jpg, IP(2).jpg ....
// 文件存放位置處理
File dir = new File("f:\\pics");
if (!dir.exists()) {
dir.mkdirs();
}
// 文件對象處理(最主要是考慮文件名的生成)
int count = 1;
File file = new File(dir, ip + "(" + (count++) + ").jpg");
while (file.exists()) {
file = new File(dir, ip + "(" + (count++) + ").jpg");
}
try {
FileOutputStream out = new FileOutputStream(file);
InputStream in = s.getInputStream();
byte buf[] = new byte[2 * 1024 * 1024];
int len = 0;
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
}
OutputStream out2 = s.getOutputStream();
out2.write("圖片文件上傳成功".getBytes());
s.close();
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}