zoukankan      html  css  js  c++  java
  • 多文件上传

    1. <form id="fileupload" action="/rest/pic/upload" method="POST" enctype="multipart/form-data">  
    2.         <!-- Redirect browsers with JavaScript disabled to the origin page -->  
    3.         <noscript><input type="hidden" name="redirect" value="https://blueimp.github.io/jQuery-File-Upload/"></noscript>  
    4.         <!-- The fileupload-buttonbar contains buttons to add/delete files and start/cancel the upload -->  
    5.         <div class="row fileupload-buttonbar">  
    6.             <div class="col-lg-7">  
    7.                 <!-- The fileinput-button span is used to style the file input field as button -->  
    8.                 <span class="btn btn-success fileinput-button">  
    9.                     <class="glyphicon glyphicon-plus"></i>  
    10.                     <span>选择文件(多选)</span>  
    11.                     <input type="file" name="uploadFile" multiple="multiple">  
    12.                 </span>  
    13.                 <button type="submit" class="btn btn-primary start">  
    14.                     <class="glyphicon glyphicon-upload"></i>  
    15.                     <span>开始上传</span>  
    16.                 </button>  
    17.                 <button type="reset" class="btn btn-warning cancel">  
    18.                     <class="glyphicon glyphicon-ban-circle"></i>  
    19.                     <span>取消上传</span>  
    20.                 </button>  
    21.                 <button type="button" class="btn btn-danger delete">  
    22.                     <class="glyphicon glyphicon-trash"></i>  
    23.                     <span>删除</span>  
    24.                 </button>   
    25.                 <input type="checkbox" class="toggle" title="全选">  
    26.                 <!-- The global file processing state -->  
    27.                 <span class="fileupload-process"></span>  
    28.             </div>  
    29.             <!-- The global progress state -->  
    30.             <div class="col-lg-5 fileupload-progress fade">  
    31.                 <!-- The global progress bar -->  
    32.                 <div class="progress progress-striped active" role="progressbar" aria-valuemin="0" aria-valuemax="100">  
    33.                     <div class="progress-bar progress-bar-success" style="0%;"></div>  
    34.                 </div>  
    35.                 <!-- The extended global progress state -->  
    36.                 <div class="progress-extended"</div>  
    37.             </div>  
    38.         </div>  
    39.         <!-- The table listing the files available for upload/download -->  
    40.         <table role="presentation" class="table table-striped"><tbody class="files"></tbody></table>  
    41.     </form>  

    Controller  层

    1. @Controller  
    2. @RequestMapping("pic")  
    3. public class FileUploadUtil{  
    4.       
    5.     private static final Logger LOGGER = Logger.getLogger(FileUploadUtil.class);  
    6.        @Value(value = "${IMAGE_BASE_URL}")  
    7.         private String IMAGE_BASE_URL;  
    8.         private String REPOSITORY_PATH;  
    9.         private static final ObjectMapper mapper = new ObjectMapper();  
    10.   
    11.         @Autowired  
    12.         private ExportService exportService;  
    13.         // 允许上传的格式  
    14.         private static final String[] IMAGE_TYPE = new String[] { ".bmp", ".jpg", ".jpeg", ".gif", ".png" };  
    15.   
    16.         @RequestMapping(value = "/upload", method = RequestMethod.POST)  
    17.         @ResponseBody  
    18.         public String upload(@RequestParam("uploadFile") MultipartFile[] uploadFile , HttpServletRequest request,HttpServletResponse response) throws Exception {  
    19.             REPOSITORY_PATH = request.getSession().getServletContext().getRealPath("upload");  
    20.             MultipartFile multipartFile = null;  
    21.             boolean isLegal = false;  
    22.             List<PicUploadResult> fileUploadResult = new ArrayList<>();  
    23.             PicUploadResult pic = null;  
    24.             ExportExcelConfig eec = new ExportExcelConfig();  
    25.             String urls = "";  
    26.             for (int i = 0; i < uploadFile.length; i++) {  
    27.                 multipartFile = uploadFile[i];  
    28.                 // 校验图片格式  
    29.                 for (String type : IMAGE_TYPE) {  
    30.                     if (StringUtils.endsWithIgnoreCase(multipartFile.getOriginalFilename(), type)) {  
    31.                         isLegal = true;  
    32.                         break;  
    33.                     }  
    34.                 }  
    35.   
    36.                 // 封装Result对象,并且将文件的byte数组放置到result对象中  
    37.                 pic = new PicUploadResult();  
    38.   
    39.                 // 状态  
    40.                 pic.setError(isLegal ? 0 : 1);  
    41.   
    42.                 // 文件新路径  
    43.                 String filePath = getFilePath(multipartFile.getOriginalFilename());  
    44.   
    45.                 if (LOGGER.isDebugEnabled()) {  
    46.                     LOGGER.debug("Pic file upload .[{}] to [{}] ."+multipartFile.getOriginalFilename());  
    47.                 }  
    48.   
    49.                 // 生成图片的绝对引用地址  
    50.                 String picUrl = StringUtils.replace(StringUtils.substringAfter(filePath,REPOSITORY_PATH), "\", "/");  
    51.                 pic.setUrl(IMAGE_BASE_URL + picUrl);  
    52.   
    53.                 File newFile = new File(filePath);  
    54.   
    55.                 // 写文件到磁盘  
    56.                 multipartFile.transferTo(newFile);  
    57.   
    58.                 // 校验图片是否合法  
    59.                 isLegal = false;  
    60.                 try {  
    61.                     BufferedImage image = ImageIO.read(newFile);  
    62.                     if (image != null) {  
    63.                         pic.setWidth(image.getWidth() + "");  
    64.                         pic.setHeight(image.getHeight() + "");  
    65.                         isLegal = true;  
    66.                     }  
    67.                 } catch (IOException e) {  
    68.                 }  
    69.   
    70.                 // 状态  
    71.                 pic.setError(isLegal ? 0 : 1);  
    72.                 if(pic.getError()==0){  
    73.                     urls+=pic.getUrl();  
    74.                     if(i<2)  
    75.                     urls+=",";  
    76.                 }  
    77.                 if (!isLegal) {  
    78.                     // 不合法,将磁盘上的文件删除  
    79.                     newFile.delete();  
    80.                 }  
    81.                 fileUploadResult.add(pic);  
    82.             }   
    83.             eec.setUrl(urls);  
    84.             eec.setCreateTime(new Date());  
    85.             exportService.addConfigInfo(eec);  
    86.             response.setContentType(MediaType.TEXT_HTML_VALUE);  
    87.             return mapper.writeValueAsString(fileUploadResult);  
    88.         }  
    89.   
    90.         private String getFilePath(String sourceFileName) {  
    91.             String baseFolder = REPOSITORY_PATH;  
    92.             Date nowDate = new Date();  
    93.             // yyyy/MM/dd  
    94.             String fileFolder = baseFolder + File.separator + new DateTime(nowDate).toString("yyyy") + File.separator + new DateTime(nowDate).toString("MM") + File.separator  
    95.                     + new DateTime(nowDate).toString("dd");  
    96.             File file = new File(fileFolder);  
    97.             if (!file.isDirectory()) {  
    98.                 // 如果目录不存在,则创建目录  
    99.                 file.mkdirs();  
    100.             }  
    101.             // 生成新的文件名  
    102.             String fileName = new DateTime(nowDate).toString("yyyyMMddhhmmssSSSS") + RandomUtils.nextInt(100, 9999) + "." + StringUtils.substringAfterLast(sourceFileName, ".");  
    103.             return fileFolder + File.separator + fileName;  
    104.         }  
    105. }  
  • 相关阅读:
    SpringCloud2.0 Eureka Client 服务注册 基础教程(三)
    美国会计准则 Generally Accepted Accounting Principles (GAAP) 与 中国会计准则
    JavaScript的订阅者模式--实现一个简单的事件监听框架
    设计模式在外卖营销业务中的实践
    20行代码做一个简易微信群发工具需要哪些单词
    南怀瑾老师:一阴一阳之谓道,是个什么道?
    怎么追女生?
    正态分布(Normal distribution)也称“常态分布”,又名高斯分布
    广义线性模型
    逻辑回归表达式
  • 原文地址:https://www.cnblogs.com/yangxiaomei/p/9081922.html
Copyright © 2011-2022 走看看