Ladebalken mit Spring MVC & DWR
Gepostet in Spring, Spring MVC
Da es nicht so richtig gute Tutorials zu demThema gibt, hab ich mir aus unterschiedlichen Tutorials was zusammengestrickt:
(Leider weiß ich die Autoren nicht mehr, sonst hätte ich sie hier natürlich erwähnt!)
Hier der Controller für die Uploadseite:
01 | public class DocumentUploadController |
02 | { |
03 | |
04 | private AjaxMultiPartResolver multiPartResolver; |
05 | |
06 | @Override |
07 | protected ModelAndView onSubmit(HttpServletRequest request, |
08 | HttpServletResponse response, Object command, BindException errors) |
09 | throws Exception |
10 | { |
11 | |
12 | ModelAndView mv = new ModelAndView( new RedirectView(getSuccessView())); |
13 | |
14 | MultipartHttpServletRequest req = multiPartResolver.resolveMultipart(request); |
15 | MultipartFile file = req.getFile( "file" ); |
16 | |
17 | //save file |
18 | return mv; |
19 | } |
20 | } |
Hier der MultiPartResolver:
01 | public class AjaxMultiPartResolver extends CommonsMultipartResolver |
02 | { |
03 | private FileUploadListener fileUploadListener; |
04 |
05 | @Override |
06 | public MultipartHttpServletRequest resolveMultipart(HttpServletRequest request) |
07 | throws MultipartException |
08 | { |
09 | String encoding = determineEncoding(request); |
10 | FileUpload fileUpload = prepareFileUpload(encoding); |
11 | if (fileUploadListener != null ) |
12 | { |
13 | fileUpload.setProgressListener(fileUploadListener); |
14 | } |
15 | try |
16 | { |
17 | List fileItems = ((ServletFileUpload) fileUpload).parseRequest(request); |
18 | MultipartParsingResult parsingResult = parseFileItems(fileItems, encoding); |
19 |
20 | return new DefaultMultipartHttpServletRequest(request, parsingResult.getMultipartFiles(), parsingResult.getMultipartParameters()); |
21 | } |
22 | catch (FileUploadBase.SizeLimitExceededException ex) |
23 | { |
24 | throw new MaxUploadSizeExceededException(fileUpload.getSizeMax(), ex); |
25 | } |
26 | catch (FileUploadException ex) |
27 | { |
28 | throw new MultipartException( "Could not parse multipart servlet request" , ex); |
29 | } |
30 | } |
31 |
32 | public FileUploadListener getFileUploadListener() |
33 | { |
34 | return fileUploadListener; |
35 | } |
36 |
37 | public void setFileUploadListener(FileUploadListener fileUploadListener) |
38 | { |
39 | this .fileUploadListener = fileUploadListener; |
40 | } |
41 | } |
Hier der FileUploadListener:
01 | public class FileUploadListener implements ProgressListener |
02 | { |
03 |
04 | private boolean multipartFinished = false ; |
05 | private UploadInfoBean uploadInfoBean; |
06 |
07 | private static long bytesTransferred = 0 ; |
08 | private static long fileSize = - 100 ; |
09 | private long totalBytesRead = 0 ; |
10 |
11 | public FileUploadListener() |
12 | { |
13 | uploadInfoBean = new UploadInfoBean(); |
14 | } |
15 |
16 | @Override |
17 | public void update( long bytesRead, long contentLength, int items) |
18 | { |
19 | totalBytesRead = bytesRead; |
20 | bytesTransferred = bytesRead; |
21 | if (fileSize != contentLength) |
22 | fileSize = contentLength; |
23 |
24 | } |
25 |
26 | public boolean isMultipartFinished() |
27 | { |
28 | return multipartFinished; |
29 | } |
30 |
31 | public void setMultipartFinished( boolean multipartFinished) |
32 | { |
33 | this .multipartFinished = multipartFinished; |
34 | } |
35 |
36 | /** |
37 | * Function is used as Javascript-DWR-Function! |
38 | * @return |
39 | */ |
40 | public UploadInfoBean getStatus() |
41 | { |
42 | uploadInfoBean.setTotalSize(fileSize/ 1024 ); |
43 | uploadInfoBean.setBytesRead(totalBytesRead/ 1024 ); |
44 | String per = NumberFormat.getPercentInstance().format(( double ) bytesTransferred / ( double ) fileSize); |
45 | uploadInfoBean.setPercentage(Integer.parseInt(per. substring( 0 , per.length() - 1 ))); |
46 | return uploadInfoBean; |
47 | } |
48 |
49 | } |
Hier die JSP, in der die Dateien hochgeladen werden:
01 | <%@ page language= "java" contentType= "text/html;charset=UTF-8" pageEncoding= "UTF-8" %> |
02 | <%@ include file= "/jsp/include.jsp" %> |
03 | <script type= "text/javascript" src= "%3C%=request.getContextPath%28%29%20%%3E/dwr/engine.js" ></script> |
04 | <script type= "text/javascript" src= "%3C%=request.getContextPath%28%29%20%%3E/dwr/util.js" ></script> |
05 | <script type= "text/javascript" src= "%3C%=request.getContextPath%28%29%20%%3E/dwr/interface/FileUploadListenerJS.js" ></script> |
06 | <script type= "text/javascript" > |
07 | function queryStatus(){ |
08 | FileUploadListenerJS.getStatus(showStatus); |
09 | return true ; |
10 | } |
11 | function overlayWhenFormFilled(){ |
12 |
13 | if ($F( 'file' ) != '' ) |
14 | { |
15 | #pageOverlayShow(); |
16 | #positionPageOverlayActionBox(); |
17 | setTimeout( 'queryStatus()' , 100 ); |
18 | return true ; |
19 | } |
20 | return false ; |
21 | } |
22 |
23 | function showStatus(status){ |
24 |
25 | //document.getElementById("progressBarSuccesfull").style.visibility = 'hidden'; |
26 | document.getElementById( "progressBar" ).style.display = "block" ; |
27 | document.getElementById( "percentage" ).innerHTML= ' ' + status.percentage; |
28 | document.getElementById( "bytesRead" ).innerHTML= ' ' + status.bytesRead; |
29 | document.getElementById( "totalSize" ).innerHTML= ' ' + status.totalSize; |
30 | document.getElementById( "progressBarBoxContent" ).style.width = parseInt(status.percentage * 4 ) + "px" ; |
31 | setTimeout(queryStatus, 100 ); |
32 |
33 | return true ; |
34 | } |
35 | </script> |
36 | <form:form method= "post" commandname= "MyCommand" enctype= "multipart/form-data" id= "MyCommand" onsubmit= "overlayWhenFormFilled();" > |
37 | <table> |
38 | <tbody><tr> |
39 | <td> |
40 | <input name= "file" id= "file" maxlength= "104857600" accept= "text/*" type= "file" ></td> |
41 | <td> |
42 | <input value= "<spring:message code=" button.save "=" ">" type= "submit" ></td> |
43 | </tr> |
44 | </tbody></table> |
45 | </form:form> |
46 | <div id= "progressBar" style= "display: none;" > |
47 | <b class = "round_border" ><b class = "round_border_layer3" ></b><b class = "round_border_layer2" ></b><b class = "round_border_layer1" ></b></b> |
48 | <div class = "round_border_content" > |
49 | <div id= "progressBarBox" > |
50 | <div id= "progressBarBoxContent" ></div> |
51 | <div id= "progressBarText" > |
52 | <spring:message code= "upload.progressartransferlabel" > |
53 | <span id= "percentage" ></span> |
54 | <spring:message code= "upload.progressbarprocentlabel" > |
55 | <spring:message code= "upload.progressbartransfersizelabel" > |
56 | <span id= "bytesRead" ></span> |
57 | <spring:message code= "upload.progressbarmetricunitlabel" > |
58 | <spring:message code= "upload.progressbarfromlabel" > |
59 | <span id= "totalSize" ></span> |
60 | <spring:message code= "upload.progressbarmetricunitlabel" > |
61 | </spring:message></spring:message></spring:message></spring:message></spring:message></spring:message></div> |
62 | <div id= "progressBarVirusScan" style= "display: none;" ><spring:message code= "upload.virusscan" ></spring:message></div> |
63 | </div> |
64 | </div> |
65 |
66 | <b class = "round_border" ><b class = "round_border_layer1" ></b><b class = "round_border_layer2" ></b><b class = "round_border_layer3" ></b></b> |
67 | </div> |
Hier die Bean, die den Status speichert:
01 | public class UploadInfoBean |
02 | { |
03 | private long totalSize = 0 ; |
04 | private long bytesRead = 0 ; |
05 | private int percentage = 0 ; |
06 |
07 | public int getPercentage() |
08 | { |
09 | return percentage; |
10 | } |
11 | public void setPercentage( int percentage) |
12 | { |
13 | this .percentage = percentage; |
14 | } |
15 |
16 | public long getTotalSize() |
17 | { |
18 | return totalSize; |
19 | } |
20 | public void setTotalSize( long totalSize) |
21 | { |
22 | this .totalSize = totalSize; |
23 | } |
24 | public long getBytesRead() |
25 | { |
26 | return bytesRead; |
27 | } |
28 | public void setBytesRead( long bytesRead) |
29 | { |
30 | this .bytesRead = bytesRead; |
31 | } |
32 | } |
Und hier die Auszüge aus der Context.xml
01 | < beans > |
02 | <!-- Handles Upload, uses commons.fileupload --> |
03 | < bean id = "multiPartResolver" class = "de.etss.core.mvc.controller.administration.documentmanagement.AjaxMultiPartResolver" > |
04 | < property name = "maxUploadSize" value = "104857600" > |
05 | < property name = "maxInMemorySize" value = "0" > |
06 | < property name = "fileUploadListener" ref = "fileUploadListener" > |
07 | </ property > |
08 |
09 | < bean id = "fileUploadListener" class = "de.xyz.core.mvc.controller.administration.documentmanagement.FileUploadListener" > |
10 | < dwr:remote javascript = "FileUploadListenerJS" > |
11 | < dwr:include method = "getStatus" > |
12 | </ dwr:include > |
13 | </ dwr:remote > |
14 | </ bean > |
15 |
16 | < dwr:configuration > |
17 | < dwr:convert type = "bean" class = "de.xyz.core.mvc.controller.administration.documentmanagement.UploadInfoBean" > |
18 | </ dwr:convert > |
19 |
20 | < dwr:controller id = "dwrController" debug = "true" ></ dwr:controller > |
21 | </ dwr:configuration ></ property ></ property ></ bean ></ beans > |
Ich hoffe, das reicht, um den Ladebalken nachzubauen…