이 기사에서는 Java가 구현 한 파일 업로드 및 다운로드 도구 클래스에 대해 설명합니다. 다음과 같이 참조에 대해 공유하십시오.
이 클래스는 Java 언어를 사용하여 일식 환경에서 파일을 업로드하고 다운로드하는 도구 클래스입니다. 서버에서 파일 이름의 복제를 피하기 위해 이전에 도입 된 C# 파일 업로드 및 다운로드 도구 클래스와 마찬가지로 서버의 파일 이름은 "서버 시간 (밀리 초로 정의 됨) + 파일 이름 + 파일 접미사"형식으로 사용됩니다. 다운로드 프로세스 중에 Spring MVC Responsentity는 파일 다운로드에 사용되며 바이트 스트림을 반환합니다. 다운로드가 성공하면 파일의 저장 경로를 사용자 정의 할 수 있습니다.
특정 소스 코드는 다음과 같습니다.
패키지 com.utils; import java.io.file; import java.io.ioexception; import java.io.inputStream; import java.io.unsupportedencodingException; import java.text.simpledateformat; import java.util.date; import org.apache.commons.io.fileutils; org.springframework.http.httpheaders; import org.springframework.http.httpstatus; import org.springframework.http.mediatepe; import org.sprameframework.http.responesentity;/*** public classe and download then gool and download thant wong hollow right and download org. 경로에 따른 디렉토리. 디렉토리가없는 경우 디렉토리를 작성하십시오 * * @param path */ private static void createir (String Path) {file filedir = new File (Path); if (! filedir.exists () &&! filedir.isdirectory ())) {// // // // directory가 filedir.mkdir ()에 있는지 결정합니다. 생성 된 (경로); 날짜 = 새 날짜 (); simpledateformat dateformat = new simpledateformat ( "yyyymmddhhmmssss"); // milliseconds string nowstr = dateformat.format (date); string filenamest = filename.substring (0, filename.lastIndexof ( ".")); // 접미사 string의 파일 이름을 제거하십시오. string expix = filename.substring (filename.lastIndexof ( ") + 1); // tothix if (filename.trim ()! 파일은 경로 + = "//" + filenamest + nowstr + "존재하지 않습니다." + 접미사; // 업로드 경로 정의} 반환 경로; }/ ** * 파일을 상기시켜 줄 * * @param filename * @param path * @return */ public static responsenity <byte []> downloadfile (문자열 파일 이름, 문자열 path) {try {filename = new String (filename.getBytes ( "gb2312"), "iso_8859_1"; (UnSupportedEncodingException e1) {e1.printstacktrace (); } 파일 파일 = 새 파일 (Path); httpheaders 헤더 = 새로운 httpheaders (); headers.setContentType (mediaType.Application_octet_stream); headers.setContentDispositionFormData ( "첨부 파일", filename); 응답 중단 <byte []> bytearr = null; try {bytearr = new responsentity <byte []> (fileUtils.ReadfileTobyTeArray (파일), 헤더, httpstatus.ok); } catch (ioexception e) {e.printstacktrace (); } return bytearr; } / ** * 입력 스트림에서 바이트 배열로 데이터를 작성 * * @param in * @return * / public static byte [] inputStream2byTearRay (inputStream in, boolean isclose) {bytearray = null; try {int total = in.available (); BytearRay = 새로운 바이트 [Total]; in.read (BytearRay); } catch (ioexception e) {e.printstacktrace (); } 마침내 {if (isclose) {try {in.close (); } catch (Exception E2) {System.out.println ( "Close Churn Failed"); }}} return bytearray; }}Java 알고리즘에 대한 자세한 내용은이 사이트에 관심이있는 독자가 주제를 볼 수 있습니다. "Java 파일 및 디렉토리 운영 기술 요약", "Java 데이터 구조 및 알고리즘에 대한 튜토리얼", "Java Operation Dom Node Skills 요약"및 "Java Cache 운영 기술 요약".
이 기사가 모든 사람의 Java 프로그래밍에 도움이되기를 바랍니다.