介绍
springMVC利用引导实现一个文件上传功能?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
监听器类FileUploadProgressListener。Java
包com.gpl.web.listener; 进口javax.servlet.http.HttpSession; 进口org.apache.commons.fileupload.ProgressListener; 进口org.springframework.stereotype.Component; 进口com.gpl.web.utils.Progress; @ component 公共类FileUploadProgressListener实现ProgressListener { 私人HttpSession会话; 公共空间setSession (HttpSession会话){ 这一点。会话=会话; 进展状态=新进展(); session.setAttribute (“status"、状态); } @Override 公共空间更新(长bytesRead, contentLength, int物品){ 进展状态=(进步)session.getAttribute (“status"); status.setBytesRead (bytesRead); status.setContentLength (contentLength); status.setItems(项目); } }
CustomerMyltipartResolver。java
包com.gpl.web.listener; 进口并不知道; 进口javax.servlet.http.HttpServletRequest; 进口org.apache.commons.fileupload.FileItem; 进口org.apache.commons.fileupload.FileUpload; 进口org.apache.commons.fileupload.FileUploadBase; 进口org.apache.commons.fileupload.FileUploadException; 进口org.apache.commons.fileupload.servlet.ServletFileUpload; 进口org.springframework.beans.factory.annotation.Autowired; 进口org.springframework.web.multipart.MaxUploadSizeExceededException; 进口org.springframework.web.multipart.MultipartException; 进口org.springframework.web.multipart.commons.CommonsMultipartResolver; 公开课CustomMultipartResolver延伸CommonsMultipartResolver { @ autowired 私人FileUploadProgressListener progressListener; 公共空间setFileUploadProgressListener (FileUploadProgressListener progressListener) { 这一点。progressListener=progressListener; } 公共MultipartParsingResult parsingResult (HttpServletRequest请求){ 字符串编码=determineEncoding(请求); FileUpload FileUpload=prepareFileUpload(编码); progressListener.setSession (request.getSession ()); fileUpload.setProgressListener (progressListener); 尝试{ ListfileItems=((ServletFileUpload) fileUpload) .parseRequest(请求); 返回parseFileItems (fileItems、编码); }(FileUploadBase。SizeLimitExceededException特异){ 把新MaxUploadSizeExceededException (fileUpload.getSizeMax()、交货); }捕捉(FileUploadException e) { 把新的MultipartException(“异常“,e); } } }
进度实体类的进步。java
包com.gpl.web.utils; 公开课进步{ 私人长bytesRead; 私人长contentLength; 私人物品; 公共长getBytesRead () { 返回bytesRead; } 公共空间setBytesRead(长bytesRead) { 这一点。bytesRead=bytesRead; } 公共长getContentLength () { 返回contentLength; } 公共空间setContentLength(长contentLength) { 这一点。contentLength=contentLength; } 公共长getItems () { 返回物品; } 公共空间setItems(长项目){ 这一点。项=物品; } @Override 公共字符串toString () { 返回“进展[bytesRead=?+ bytesRead +“, contentLength=?+ contentLength +“,物品=?+项目+“]”; } }
春配置文件需加入如下豆
& lt; !——文件上传——比; & lt; bean id=癿ultipartResolver"类=癱om.gpl.web.listener.CustomMultipartResolver"比; & lt;属性名=癲efaultEncoding"值=https://www.yisu.com/zixun/" utf - 8 "> 属性> <属性名=" maxUploadSize " value=" 838860800 "> 属性> 豆>
控制器层实现
/* * *文件上传 * @param请求 * @param响应 * @param文件 * @throws IOException */@RequestMapping (value=https://www.yisu.com/zixun/?上传”,方法=RequestMethod.POST) 公共空间上传(HttpServletRequest请求,HttpServletResponse响应,@RequestParam(“文件”)CommonsMultipartFile文件) { 尝试{ PrintWriter; 布尔标志=false; 如果(file.getSize ()> 0) { 字符串路径="/断言/上传/文件/excel/? 字符串uploadPath=request.getSession () .getServletContext () .getRealPath(路径); System.out.println (uploadPath); FileUtils.copyInputStreamToFile (file.getInputStream(),新文件(uploadPath, file.getOriginalFilename ())); 国旗=true; }=response.getWriter (); 如果(标志==true) { 都会(" 1 "); 其他}{ 都会(“2”); } }捕捉(异常e) { e.printStackTrace (); } }springMVC利用引导实现一个文件上传功能