注:这仅仅是一个粗略笔记。有些代码可能没用。兴许会再更新一个能够使用的版本号。不足之处,敬请见谅。
1。spring环境搭建,这里使用的是spring3的jar,须要同一时候引入common-IO 和common-fileupload的jar包。
1.1spring.XXX.xml文件配置bean
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="${web.maxUploadSize}" /> </bean>
value="xxxx"依据实际情况改动。
2.html表单
<form id="uploadForm" action="data/myData4PipeiUpload" method="post" enctype="multipart/form-data"> <div class="upload_box"> <div class="upload_main"> <div class="upload_choose"> <span id="fileDragArea" class="upload_drag_area">请将文件拖拽至此处上传</span> <input id="fileImage" size="30" name="fileselects[]" multiple="multiple" type="file"> </div> <div id="preview" class="upload_preview"></div> </div> <div id="uploadInf" class="upload_inf"></div> </div> <div class="upload_box"> 文件名2: <input name="fileFieName" id="fileFieName" /> </div> <DIV class="modal-footer"> <BUTTON class="btn btn-primary" type="submit" id="submitBut3" data-dismiss="modal">上传数据文件</BUTTON> </DIV> </form>
2.1除了普通上传的一些格式要求之外。这里要注意多文件须要name=“XXX[]” multiple=“multiple"
<input id="fileImage" size="30" name="fileselects[]" multiple="multiple" type="file">
2.2这里使用了upload.js和zxxFile.js。去配置相关參数
3.后台代码编写两种方式。
3.1
@RequestMapping(value = "/myData4PipeiUpload", method = RequestMethod.POST) public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { // 转型为MultipartHttpRequest: MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; List<MultipartFile> singleFiles = multipartRequest.getFiles("fileselects[]"); if(singleFiles!=null&&singleFiles.size()>0){ for(MultipartFile file: singleFiles){ // 获得文件名称: String filename = file.getOriginalFilename(); // 获得输入流: InputStream input = file.getInputStream(); // 写入文件 // 或者: File source = new File("d:/dd/"+filename); file.transferTo(source); } } }
注意參数的获取要使用fileselects[]去获取列表
3.2,直接将请求名称写入到@requestParam
@RequestMapping(value = "/myData4PipeiUpload", method = RequestMethod.POST) public void handleRequest(@RequestParam("fileselects[]") MultipartFile[] files,HttpServletRequest request, HttpServletResponse response) throws Exception { if(files!=null&&files.length>0){ for(MultipartFile file: files){ // 获得文件名称: String filename = file.getOriginalFilename(); // 获得输入流: InputStream input = file.getInputStream(); // 写入文件 // 或者: File source = new File("d:/dd/"+filename); file.transferTo(source); } } }
这样子就能够再进行兴许的操作。
时间: 2024-10-12 07:22:10