이 기사의 예제는 참조에 대한 Java 이미지 업로드 코드를 공유합니다. 특정 내용은 다음과 같습니다
java.io.*; java.net.*; /**end end*/ class picsend {public static void main (String [] args)은 예외 {if (args.length! = 1) {System.out.println ( ".jpg 사진을 선택하십시오"); 반품; } 파일 파일 = 새 파일 (args [0]); if (! 반품; } 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); // 서비스 생성 FILEINPUTSTREAM FIS = NEW FILEINPUTSTREAM ( "D : //Beauty.jpg"); // outputStream을 읽습니다. int len = 0; while ((len = fis.read (b))! = -1) {out.write (b, 0, len); } s.shutdownoutput (); // tag end inputStream in = s.getInputStream (); // 서버 읽기 데이터 byte [] bin = new Byte [1024]; int num = in.read (bin); System.out.println (새 문자열 (bin, 0, num)); fis.close (); s.close (); }} class picthread는 실행 가능한 {private socket s; picthread (소켓 s) {this.s = s; } public void run () {int count = 1; 문자열 ip = s.getInetAddress (). gethostAddress (); // ip try {system.out.println (ip+".......... Connect"); inputStream in = s.getInputStream (); // 스트림 파일에서 데이터를 읽습니다. 파일 = 새 파일 (ip+"("+(count)+")"+". jpg"); while (file.exists ()) // 파일이 존재하는지 파일 = 새 파일 (ip+"("+(count ++)+")"+". jpg"); fileoutputStream fos = new FileOutputStream (file); // byte write [] b = new Byte [1024]; int len = 0; while ((len = in.read (b))! = -1) {fos.write (b, 0, len); } outputStream out = s.getOutputStream (); // 서버 out.write에 전송 된 데이터 쓰기 ( "성공적으로 업로드되었습니다!".getBytes ()); fos.close (); s.close (); } catch (예외 e) {새로운 runtimeexception 던지기 ( "업로드 실패"); }}} /**server* / class picrece {public static void main (String [] args)은 예외 {serversocket ss = new serversocket (10006); while (true) {socket s = ss.accept (); // 새 스레드를받습니다 (new picthread (s)). start (); }}}효과:
위의 내용은이 기사에 관한 모든 것입니다. 모든 사람들이 Java 프로그래밍을 배우는 것이 도움이되기를 바랍니다.