この記事の例は、参照用のJava画像アップロードコードを共有しています。特定のコンテンツは次のとおりです
java.io.*をインポートします。 java.net。*; /**end end*/ class picsend {public static void main(string [] args)throws exception {if(args.length!= 1){system.out.println( "a .jpg画像を選択してください");戻る; } file file = new file(args [0]); if(!(file.exists()&& file.isfile())){system.out.println( "画像に問題があります(ファイルではないか、存在しない)");戻る; } if(!file.getName()。endswith( "。jpg")){system.out.println( "画像形式が間違っているので、画像を再選択してください");戻る; } if(file.length()> 1024*1024*10){system.out.println( "画像が大きすぎてアップロードできない");戻る; } socket s = new Socket( "192.168.33.1"、10006); // service fileinputStream fis = new fileinputStream( "d://beauty.jpg"); //画像outputStream outを読む= s.getOutputStream(); int len = 0; while((len = fis.read(b))!= -1){out.write(b、0、len); } s.ShutDownOutput(); // Tag End inputstream in = s.getInputStream(); // read server returens data byte [] bin = new byte [1024]; int num = in.read(bin); System.out.println(new String(bin、0、num)); fis.close(); s.close(); }} class picthread runnable {private socket s; picthread(socket s){this.s = s; } public void run(){int count = 1; string ip = s.getinetaddress()。gethostaddress(); // get ip try {system.out.println(ip+".......... connect"); inputStream in = s.getInputStream(); //ストリームファイルのデータを読み取り= new file( "+(count)+") "+"。JPG "); while(file.exists())//ファイルが存在するかどうかを決定しますfile = new file( "+(count ++)+") "+"。JPG "); fileoutputStream fos = new fileoutputStream(file); // write byte [] b = new byte [1024]; int len = 0; while((len = in.read(b))!= -1){fos.write(b、0、len); } outputStream out = s.getOutputStream(); // Serverに送信されたデータを書き込みout.write( "Uploaded upload!" .getBytes()); fos.close(); s.close(); } catch(Exception e){throw new runtimeException( "upload failed"); }}} /**server* / class picrece {public static void main(string [] args)throws exception {serversocket ss = new Serversocket(10006); while(true){socket s = ss.accept(); //新しいスレッド(new picthread(s))。start(); }}}効果:
上記はこの記事に関するものです。誰もがJavaプログラミングを学ぶことが役立つことを願っています。