다운로드 :
1. Spring-MVC에서 구성 (100m 미만의 파일 다운로드)
<ean> <property name = "messageconverters"> <slest> <!-다운로드 리턴 유형 구성-> <bean/> <ean> <!-인코딩 메소드 구성-> <property name = "supportedMediAtypes"value = "application/json; charset = utf-8"/> </bean> </bean>
파일 코드를 다운로드하십시오
@requestmapping ( "/file/{name.rp}") public responseentity <byte []> filedownload (@pathvariable ( "name.rp") 문자열 이름, httpservletrequest request, httpservletrepsonse response) {// @pathvariable 문자열 이름, // @requestparam ( "name") 문자열 이름, // System.out.println ( "<name>"+name); // system.out.println ( ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >> re = re = null; CSS, JS, JSON, GIF, PNG, BMP, JPG, ICO, DOC, DOC, DOCX, XLSX, TXT, SWF, PDF*** // 다운로드, 정적 로딩 간섭 느낌 F = New Feelutile (); Name = F.GetFileformat (이름); String PathString = "C : // Tempdirectory///"+; 파일 = 새 파일 (pathString); httpheaders headers = new httpheaders (); // string filename = urlencoder.encode (이름, "utf-8"); // garred 중국어 이름 string filename의 문제를 해결하려면 new String ( "utf-8"), "utf-8"; byte [] by = fileUtils.ReadFileToByteArray (file); headers.setContentType (mediaType.application_octet_stream); // urlencoder.encode (filename, "utf-8") headers.setContentDispositionFormData ( "첨부 파일", filename); headcontlength (by.length); responseentity <byte []> (By, Headers, httpstatus.created);} catch (예외 e) {e.printstacktrace (); try {request.getRequestDispatcher ( "/error/404.jsp"). Forward (요청, 응답);} catch (servletexception e1) {// auto auto genated catch. blocke1.printstacktrace ();} catch (ioexception e1) {// todo 자동 생성 캐치 블록 1.printstacktrace ();}} return re;}파일 업로드 :
스프링 -MVC의 1 컨퍼런스
<!-4. 파일 업로드 구성 파일 업로드-> <bean id = "multipartresolver"class = "org.springframework.web.multipart.commons.commonsmultipartresolver"> <property name = "defaultencoding"> value> utf-8 </value> <property 이름 = "maxuploadsize"> <value> 1048576000 </value> </property> <property name = "maxinmemorysize"> <value> 40960 </value> </property> </bean>
컨트롤러의 코드는 다음과 같습니다
@requestmapping (value = "/upload", method = requestmethod.post) @ResponseBodyPublic JSON UPLOAD (DOC, DOC, @RequestParam ( "uploadfile") CommonSmultipartFile 파일) {JSON J = new JSON (); try {String realPath = this.servletContext.getRealPath ( "/upload"); String uploadFileFileName = file.getoriginalFilename (); String uploadFileFileNameWitHoutSpace = uploadFileFileName.Replaceall ( "", ""); String FilEtype = uploadFileFileNameAwitHoutSpace.SubString (uploadFileFilenAmeWitHoutSpace.lastIndexOf ( ")); 파일 targetFile = 새 파일 (realPath+file.separator, uploadFileFileFileNameWithOutsPace); if (targetFile.exists ()) {targetFile.delete ();} file.getFileItem (). 쓰기 (targetFile); docservice.upload (doc, uploadfilefilenamewithoutspace); j.setsuccess (true); j.setmsg ( "성공적으로 업로드 매뉴얼");} catch (예외 e) {logger.error (exception.getexceptionmessage (e));위의 것은 Java Spring MVC 업로드 및 다운로드 파일 구성 및 컨트롤러 메소드에 대한 자세한 설명입니다. 모든 사람에게 도움이되기를 바랍니다. 궁금한 점이 있으면 메시지를 남겨 주시면 편집자가 제 시간에 모든 사람에게 답장을 드리겠습니다. Wulin.com 웹 사이트를 지원해 주셔서 대단히 감사합니다!