拾荒者

只要去,就必到达!

页面使用form表单提交,后台使用springmvc接收实现文件上传功能的实现以及问题

业务需求:

需要使用excel模版(提供下载),并导入录好的excel信息,解析excel入库。

分析出的主要功能:

1.提供excel模版下载

2.上传文件

3.解析excel,并存储数据到库

代码如下:

页面:

<div class="modal fade" id="importDiv" tabindex="-1" role="dialog">
		<div class="modal-dialog" role="document">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal"
						aria-label="Close">
						<span aria-hidden="true">×</span>
					</button>
					<h4 class="modal-title">导入索引</h4>
				</div>
				<div class="modal-body">
					<div style="width: 600px; padding: 10px; height: auto; min-height: 100px; max-height: 600px; overflow-x: hidden; overflow-y: auto;">
								
							<div class="selectArea">
								<span>类型:</span>
								 	<select id="classfication_excel" >
                                      <option value="work">著作</option>
                                      <option value="periodical">期刊论文</option>
                                      <option value="degree">学位论文</option>
                                      <option value="journal">报刊文章 </option>
                                      <option value="separateOut">析出文献 </option>
                                    </select>
							 </div>
							
							<div class="warp100">
								<div class="selectFile">
								<form method="post" enctype="multipart/form-data" id="file_form" th:action="@{/administrator/indexDataBaseManager/importData}" >
									<span>选择文件:</span>
									<input  id="excel_type" name="excel_type" type="hidden"></input>
									<input type="file" id="tempFile" name="tempFile"></input>
										
								</form>
								</div>
							</div>
					</div>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-primary" onclick="downloadModel()">下载模版</button>
					<button type="button" class="btn btn-primary" onclick="importData()">导入</button>
					<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
				</div>
			</div>
		</div>
	</div>


js代码如下:

	function downloadModel(){
					var type=$("#classfication_excel").val();
					window.location.href=getRootPath()+'/administrator/indexDataBaseManager/downloadModel?type='+type;
				}
				
				function importData(){
					var type=$("#classfication_excel").val();
					var type_name=$("#classfication_excel option:selected").text();
					var file_name=$("#tempFile").val();
					var dress=file_name.substr(file_name.length-4);
					if(file_name==""||file_name==null){
						layer.msg("请选择一个文件"); 
						return
					}
					if(dress!=".xls"){
						layer.msg("文件格式有误,请选择后缀为'.xls'的文件。"); 
						return
					}
					
					layer.confirm('类型为:'+type_name+",文件为:"+file_name+",确定导入吗?", {title:'警告',
						  btn: ['确定','取消'] //按钮
					}, function(){
						layer.msg("导入中,稍后请刷新页面查看导入结果!",{time:2000}); 
						$("#excel_type").val(type);
						setTimeout(function()
								{
							$('#file_form').submit();
								},1000);
					
					}, function(){
					}
					
					);
			
					
				}

后台springmvc代码:

	@RequestMapping(value = "/downloadModel", method = RequestMethod.GET)
	public ResponseEntity<byte[]> downloadModel(@RequestParam(value = "type")String type) throws IOException{
		
		String fileName="";
		if(type.equals("work")){
			fileName="著作.xls";
		}else if(type.equals("periodical")){
			fileName="期刊论文.xls";
		}else if(type.equals("degree")){
			fileName="学位论文.xls";
		}else if(type.equals("journal")){
			fileName="报刊文章.xls";
		}else if(type.equals("separateOut")){
			fileName="析出文献.xls";
		}
		String path=indexTemplate+fileName;
		File file =new File(path);
	       HttpHeaders headers = new HttpHeaders();
	       fileName=new String(fileName.getBytes("UTF-8"),"iso-8859-1");
	        headers.setContentDispositionFormData("attachment", fileName);   
	        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
	        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),    
	                                          headers, HttpStatus.CREATED); 
	}
	@RequestMapping(value = "/importData", method = RequestMethod.POST)
	public String importData(@RequestParam(value = "excel_type") final String excel_type, @RequestParam(value = "tempFile") final MultipartFile tempFile) {
	    
		
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				try {
			
				 Map<String, String> fieldMap = new LinkedHashMap<String, String>();
			        if(excel_type.equals("work")){
			    		fieldMap.put("责任者", "author");
			    		fieldMap.put("题名", "title");
			    		fieldMap.put("语言", "indexLang");
			    		fieldMap.put("学科", "indexTag");
			    		fieldMap.put("出版地", "city");
			    		fieldMap.put("出版单位", "press");
			    		fieldMap.put("出版时间", "pressDate");
			    		fieldMap.put("链接地址", "url");
			        }else if(excel_type.equals("periodical")){
			    		fieldMap.put("责任者", "author");
			    		fieldMap.put("题名", "title");
			    		fieldMap.put("语言", "indexLang");
			    		fieldMap.put("学科", "indexTag");
			    		fieldMap.put("出版地", "city");
			    		fieldMap.put("期刊名", "press");
			    		fieldMap.put("出版时间", "pressDate");
			    		fieldMap.put("页码", "page");
			    		fieldMap.put("链接地址", "url");
			        }else if(excel_type.equals("degree")){
			        	fieldMap.put("责任者", "author");
			    		fieldMap.put("题名", "title");
			    		fieldMap.put("论文性质", "property");
			    		fieldMap.put("语言", "indexLang");
			    		fieldMap.put("学科", "indexTag");
			    		fieldMap.put("出版地", "city");
			    		fieldMap.put("学校", "press");
			    		fieldMap.put("出版时间", "pressDate");
			    		fieldMap.put("链接地址", "url");
			        }else if(excel_type.equals("journal")){
			        	fieldMap.put("责任者", "author");
			    		fieldMap.put("题名", "title");
			    		fieldMap.put("语言", "indexLang");
			    		fieldMap.put("学科", "indexTag");
			    		fieldMap.put("出版地", "city");
			    		fieldMap.put("报纸名称", "press");
			    		fieldMap.put("出版时间", "pressDate");
			    		fieldMap.put("版次", "version");
			    		fieldMap.put("链接地址", "url");
			        }else if(excel_type.equals("separateOut")){
			        	fieldMap.put("责任者", "author");
			    		fieldMap.put("题名", "title");
			    		fieldMap.put("原著题名", "originalTitle");
			    		fieldMap.put("编者", "originalAuthor");
			    		fieldMap.put("语言", "indexLang");
			    		fieldMap.put("学科", "indexTag");
			    		fieldMap.put("出版地", "city");
			    		fieldMap.put("出版单位", "press");
			    		fieldMap.put("出版时间", "pressDate");
			    		fieldMap.put("页码", "page");
			    		fieldMap.put("链接地址", "url");
			        }
					
					List<IndexExcelDTO> list = new ArrayList<IndexExcelDTO>();
					try {
						list = ExcelUtil.excelToList_IndexInfoDTO(tempFile.getInputStream(), 0, fieldMap, null);
						System.out.println(list.size());
					} catch (ExcelException | IOException e) {
						e.printStackTrace();
					}
					List<Index> indexs=new ArrayList<Index>();
					 for(IndexExcelDTO dto:list){
						 Index index=new Index();
						 ArrayList<String> langs=new ArrayList<String>();
						 ArrayList<String> tags=new ArrayList<String>();
						 BeanUtils.copyProperties(dto, index);				 
						 langs.add(dto.getIndexLang());
						 tags.add(dto.getIndexTag());
						 index.setIndexLang(langs);
						 index.setIndexTag(tags);
						 index.setClassfication(excel_type);
						 index.setCreateDate(new Date());
						 indexs.add(index);
					 }
					List<Index> saved=(List<Index>) indexRepository.save(indexs);
					if(null!=saved && saved.size()!=0){
						log.info("导入成功,类型为{},导入个数为:{},文件名为:{}",excel_type,saved.size(),tempFile.getOriginalFilename());
					}else{
						log.info("导入失败,类型为{},文件名为:{}",excel_type,tempFile.getOriginalFilename());
					}
					
				} catch (Exception e) {
					log.error("导入出错!类型为{},文件名为:{}", e,excel_type,tempFile.getOriginalFilename());
				}
			}
		}).start();
      
	    return "redirect:/administrator/indexDataBaseManager/list";
	}
	

问题:

之前我在导入excel的springmvc的代码中 返回的是:

/administrator/indexDataBaseManager/list 

而未加上重定向的redirect 

这导致我在导完数据后,页面地址还是指向方法,我在刷新页面的时候,会有一个问题:表单重复提交。。。经过各种尝试,使用redirect 解决了这个问题。  写此文以记之。
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34246546/article/details/79974865
文章标签: spring mvc
个人分类: springmvc
上一篇springcloud学习笔记(十三)最近feign踩的一个坑
下一篇学好数据库,看这9本书就够了
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭