이 기사의 예제는 참조를 위해 Java Webservice의 업로드 및 다운로드를위한 코드를 공유합니다. 특정 내용은 다음과 같습니다
1. 새로운 동적 웹 프로젝트를 만들고 Mefileserver를 만들고 새 패키지 COM을 만들고 새 클래스 파일 프로 로그레이션을 만듭니다.
패키지 com; import java.io.fileInputStream; import java.io.fileoutputStream; import java.io.ioexception; import java.sql.date; import java.text.simpledateformat; import java.util.random; import sun.misc.base64decoder; * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * import java.util.random; 파일 업로드 및 다운로드*/public class fileprogress {public String sayhello (문자열 이름) {return "hello,"+name+"/n"+getDir ( "2"); }/** 파일 업로드 서비스*/public string uploadfile (문자열 파일 이름, 문자열 파일 타입, 문자열 파일) // byte [] bytes) {fileoutputStream fos = null; try {string filedir = getDir (filetype); Base64decoder decoder = 새로운 base64decoder (); 바이트 [] bytes = decoder.decodebuffer (파일); if (filedir == "") {return ""; } integer rdm = new random (). NextInt (10000); String savename = getDatatimestring (true) +rdm.tostring () +filename.substring (filename.indexof ( '.')); FOS = 새 FILEOUTPUTSTREAM (filedir+savename); // 바이트 배열 바이트에서 파일 출력 스트림 fos.write (bytes)에 데이터를 작성합니다. fos.flush (); Return Filedir +Savename; } catch (예외 e) {e.printstacktrace (); 반품 ""; } 마침내 {try {fos.close (); } catch (ioexception e) {e.printstacktrace (); }}}/** * @param filepath */private String getDir (String filetype) {String Path = "f : // youme // {0} //" + getDatastring () + "//"; switch (filetype) {case "2": path = path.replace ( "{0}", "image"); 부서지다; 사례 "3": Path = Path.Replace ( "{0}", "Vedio"); 부서지다; 기본값 : 반환 ""; } try {java.io.file file = new java.io.file (path); if (! file.exists ()) {if (! file.mkdirs ()) {return ""; }} 반환 경로; } catch (예외) {return ""; } 마침내 {}}/** 파일 다운로드 서비스*/public string downloadFile (String FilePath) {// filepath = "f : //youme//2013-09-03//201309031700143294.amr"; fileInputStream in = null; 바이트 바이트 [] = null; 문자열 파일 = null; try {in = new FileInputStream (FilePath); 바이트 = 새로운 바이트 [in.available ()]; // 입력 스트림에서 BYTES.LENGTH의 데이터의 바이트를 바이트 배열 바이트 바이트 in.Read (bytes)로 읽습니다. Base64encoder encoder = 새로운 Base64encoder (); file = encoder.encode (bytes); } catch (예외 e) {e.printstacktrace (); 반품 ""; } 마침내 {try {in.close (); } catch (ioexception e) {e.printstacktrace (); }} // return bytes; 반환 파일; } /** 현재 시간을 가져옵니다* / private static string getDataMestring (boolean isfilename) {try {simpledateformat formatter = null; if (! isfilename) {formatter = new simpledateformat ( "yyyy-mm-dd hh : mm : ss"); } else {formatter = new simpledateformat ( "yyyymmddhhmmss"); } date curdate = new 날짜 (System.CurrentTimeMillis ()); // 현재 시간을 얻습니다. formatter.format (Curdate); } catch (Exception Ex) {System.out.println (ex.getMessage ()); 반품 ""; }} /** 현재 날짜를 가져옵니다* / private static string getDatastring () {try {simpledateformat formatter = new SimpledateFormat ( "yyyy-mm-dd"); 날짜 Curdate = 새 날짜 (System.CurrentTimeMillis ()); // 현재 시간을 얻습니다. formatter.format (Curdate); } catch (Exception Ex) {System.out.println (ex.getMessage ()); 반품 ""; }}} 2. 테스트 웹 서비스 게시 (Eclipse java ee ide)
방금 만든 클래스를 마우스 오른쪽 버튼으로 클릭, 이제 webService-> 웹 서비스 생성, 웹 서비스 유형 상향식 Upjava Bean 웹 서비스, 서비스 구현 선택 대상 클래스를 선택하고 다음 단계를 선택할 때까지 게시 할 메소드 인터페이스를 선택하십시오. Tomcat이 시작되었습니다. 방금 생성 된 WSDL 폴더에서 WSDL 파일을 마우스 오른쪽 버튼으로 클릭하고 웹 서비스 테스트를 선택한 다음 해당 메소드의 매개 변수를 입력하십시오.
위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.