A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 寐海流风 中级黑马   /  2014-6-25 20:59  /  744 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 寐海流风 于 2014-6-26 09:58 编辑

怎么能实现自动获取文件名和文件扩展名呢?代码如下,请高手检阅!
  1. public class Test9 {
  2.         
  3.         public static void main(String[] args) throws Exception {
  4.                
  5.                 TextClient.uploadFile();
  6.         }
  7. }

  8. //客户端
  9. class TextClient{
  10.         
  11.         public static void uploadFile(){
  12.                
  13.                 Socket socket = null;
  14.                 FileInputStream fis = null;
  15.                 OutputStream out = null;
  16.                 InputStream in = null;
  17.                
  18.                 //"D:\\JavaProject\\exam\\34.jpg"
  19.                 File file = new File("D:\\JavaProject\\exploration&discovery\\abc.jpg");
  20.                
  21.                 try{
  22.                         //"192.168.0.108"
  23.                         socket = new Socket(InetAddress.getLocalHost(),10033);
  24.                         
  25.                         fis = new FileInputStream(file);
  26.                         out = socket.getOutputStream();        
  27.                                        
  28.                         String fileName = file.getName();
  29.                         
  30.                         System.out.println(fileName.toString());
  31.                         
  32.                         out.write((fileName+"\r").getBytes());
  33.                         
  34.                         byte[] bufOut = new byte[1024];
  35.                         int len = 0;
  36.                         
  37.                         while((len=fis.read(bufOut))!=-1){
  38.                                 out.write(bufOut,0,len);
  39.                         }
  40.                         
  41.                         socket.shutdownOutput();
  42.                         
  43.                         in = socket.getInputStream();
  44.                         
  45.                         byte[] bufIn = new byte[1024];
  46.                         int num = in.read(bufIn);
  47.                         
  48.                         System.out.println(new String(bufIn,0,num));
  49.                         
  50.                 }catch(UnknownHostException e){
  51.                         System.out.println("获取本地主机IP地址失败!");
  52.                 }catch(IOException e){
  53.                         System.out.println(e.toString());
  54.                 }finally{
  55.                         
  56.                         if(fis!=null){
  57.                                 try{
  58.                                        
  59.                                         fis.close();
  60.                                 }catch(IOException e){
  61.                                         System.out.println("关闭输入流失败!");
  62.                                 }
  63.                         }
  64.                         if(socket!=null){
  65.                                 try{
  66.                                        
  67.                                         socket.close();
  68.                                 }catch(IOException e){
  69.                                         System.out.println("关闭套接字流失败!");
  70.                                 }
  71.                         }
  72.                 }
  73.         }
  74. }

  75. //服务端
  76. class TextServer{
  77.         
  78.         public static void main(String[] args) throws IOException {
  79.                
  80.                 @SuppressWarnings("resource")
  81.                 ServerSocket serverSocket = new ServerSocket(10033);
  82.                
  83.                 while(true){
  84.                         
  85.                         final Socket socket = serverSocket.accept();
  86.                         
  87.                         new Thread(new Runnable(){

  88.                                 @Override
  89.                                 public void run() {
  90.                                        
  91.                                         int count = 1;
  92.                                        
  93.                                         InputStream in = null;
  94.                                         FileOutputStream fos = null;
  95.                                         OutputStream out = null;
  96.                                        
  97.                                         try{
  98.                                                 
  99.                                                 String ip = socket.getInetAddress().getHostAddress();
  100.                                                 System.out.println(ip + "...connected.");
  101.                                                 
  102.                                                 in = socket.getInputStream();
  103.                                                 
  104.                                                 int by = 0,x = 0;
  105.                                                 byte[] bytes = new byte[1024];
  106.                                                 while((by=in.read())!=13){
  107.                                                         bytes[x++] = (byte) by;
  108.                                                 }
  109.                                                 String fileName = new String(bytes,"GBK");
  110.                                                 
  111.                                                 System.out.println(fileName.toString());
  112.                                                 String fileExtension = fileName.substring(fileName.lastIndexOf('.')+1);
  113.                                                 
  114.                                                 System.out.println(fileExtension.toString());
  115.                                                 
  116.                                                 File file = new File(ip+"("+count+")."+fileExtension);
  117.                                                 while(file.exists())
  118.                                                         file = new File(ip+"("+(count++)+")."+fileExtension);
  119.                                                 
  120.                                                 
  121.                                                 fos = new FileOutputStream(file);
  122.                                                 
  123.                                                 byte[] buf = new byte[1024];
  124.                                                 int len = 0;
  125.                                                 while((len=in.read(buf))!=-1){
  126.                                                         fos.write(buf,0,len);
  127.                                                 }
  128.                                                 out = socket.getOutputStream();
  129.                                                 out.write("上传成功!".getBytes());
  130.                                                 
  131.                                                 
  132.                                                 fos.close();
  133.                                                 socket.close();
  134.                                                 
  135.                                         }catch(Exception e){
  136.                                                 throw new RuntimeException("文件上传失败!");
  137.                                         }                                
  138.                                 }
  139.                         //启动线程
  140.                         }).start();
  141.                 }               
  142.         }
  143. }
复制代码

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马