基于bootstrap-fileinput实现文件上传,下载(通过spring mvc)

<script type="text/javascript" src="<%=path %>/js/jquery-3.1.0.min.js"></script>
<link rel="stylesheet" href="<%=path %>/statics/libs/upload/css/fileinput.min.css" />
<script src="<%=path %>/statics/libs/upload/js/fileinput.min.js" type="text/javascript"></script>
<script src="<%=path %>/statics/libs/upload/js/locales/zh.js" type="text/javascript"></script>

<script type="text/javascript">
$(document).ready(function () {
	$('#mdmUpload').fileinput({
	    language: 'zh',
	    uploadUrl: './controller.do?method=aaaa',
	    allowedFileExtensions: ['xlsx'],
	    showCaption: false,
	    showUpload: true,
	    maxFileCount: 3, 
	    enctype: 'multipart/form-data',
	    validateInitialCount:true,
        previewFileIcon: "<i class='glyphicon glyphicon-king'></i>",
        msgFilesTooMany: "选择上传的文件数量({n}) 超过允许的最大数值{m}!",
	    browseClass: 'btn btn-primary',
	    uploadExtraData:function (previewId, index) {           //传参
        	var data = {
                //"reportGroupId": $('#id').html(),      //此处自定义传参
            };
            return data;
         }
	});
	 $("#aaaUpload").on("fileuploaded", function (event, data, previewId, index) {
		 $('#upload-tip-form-message-aaa').html(data.response.msg);
		 $('#show_tip_aaa').click();
		
     });
});

</script>

<input type="file" id="aaaUpload" name="aaaUpload" multiple />
public class ExcelUtil {

	public static String getUploadPath(HttpServletRequest request) {
		String appContext = request.getSession().getServletContext()
				.getRealPath("/");
		String uploadPath = appContext + "upload" + File.separator;
		return uploadPath;
	}
	
	public static String getDownloadPath(HttpServletRequest request,String fileName) {
		String appContext = request.getSession().getServletContext()
				.getRealPath("/");
		StringBuffer pathBuilfer=new StringBuffer();
		pathBuilfer.append(appContext);
		pathBuilfer.append("template");
		pathBuilfer.append(File.separator);
		pathBuilfer.append(fileName);
		return pathBuilfer.toString();
	}

	public static List<String> getRepeatDataGroup(List<String> list) {
		List<String> result = new ArrayList<>();
		Map<String, List<Integer>> map = new HashMap<String, List<Integer>>();
		for (int i = 0; i < list.size(); i++) {
			if (map.containsKey(list.get(i))) {
				List<Integer> lis = map.get(list.get(i));
				lis.add(i + 2);
				map.put(list.get(i), lis);
			} else {
				List<Integer> lis = new ArrayList<Integer>();
				lis.add(i + 2);
				map.put(list.get(i), lis);
			}
		}

		if (map.size() < list.size()) {
			for (Map.Entry<String, List<Integer>> tmp : map.entrySet()) {
				if (tmp.getValue().size() > 1) {
					result.add(tmp.getValue().toString());
				}
			}
		}
		return result;
	}
}
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
				request.getSession().getServletContext());
		String uploadPath = "";
		// File Upload
		if (multipartResolver.isMultipart(request)) {
			MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
			Iterator<String> iter = multiRequest.getFileNames();
			while (iter.hasNext()) {
				MultipartFile file = multiRequest.getFile(iter.next()
						.toString());
				if (file != null) {
					uploadPath = ExcelUtil.getUploadPath(request)
							+ file.getOriginalFilename();
					try {
						file.transferTo(new File(uploadPath));
					} catch (Exception e) {
						logger.info("Upload Exception ";
						tipMsg.append("文件上传失败!");
						executeState = false;
					}
				}

			}
		}
@RequestMapping(params = "method=exportTemplate")
	@ResponseBody
	public void exportMdmTemplate(HttpServletRequest request,
			HttpServletResponse response) {
		String fileName = "Template.xlsx";
		String path = ExcelUtil.getDownloadPath(request, fileName);
		response.setCharacterEncoding("utf-8");
		response.setContentType("application/octet-stream");
		response.setHeader("Content-Disposition", "attachment;fileName="
				+ fileName);
		InputStream inputStream = null;
		OutputStream os = null;
		try {
			inputStream = new FileInputStream(new File(path));
			os = response.getOutputStream();
			byte[] b = new byte[1024];
			int length;
			while ((length = inputStream.read(b)) > 0) {
				os.write(b, 0, length);	
			}
		} catch (Exception e) {
			logger.info("Download Template Exception ";
		} finally {
			try {
				os.close();
				inputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
bootstrap-fileinput是一种基于Bootstrap框架的文件上传插件,它提供了简单易用的界面和丰富的功能。 首先,bootstrap-fileinput具有美观的样式,与Bootstrap的整体风格相匹配。它使用了一系列清晰、直观的图标和按钮,让用户可以方便地浏览、选择和上传文件。 其次,bootstrap-fileinput还支持多种文件展示方式。它可以以列表或网格的形式显示文件预览,在列表模式下可以显示文件的名称、大小、类型等信息,而在网格模式下则可以直观地展示文件的缩略图。这种多样化的展示方式可以让用户根据自己的需求选择最适合的视觉效果。 此外,bootstrap-fileinput还具有丰富的功能。它支持文件的多选和拖拽上传,可以通过设置参数来限制文件的大小、类型和数量等。它还提供了预览、删除和重新选择文件的功能,让用户可以在上传之前查看文件的内容和清理不需要的文件。此外,它还可以通过AJAX异步上传文件,实现无刷新上传的效果。 最后,bootstrap-fileinput还提供了一些额外的扩展功能。例如,它可以通过设置参数来自定义上传按钮的样式和文本,可以根据不同的需求注册和调用一系列的回调函数,以便实现更加灵活的操作。 总之,bootstrap-fileinput是一个功能强大、界面美观的文件上传插件,它可以为用户提供便捷的文件管理和上传体验。不仅仅可以用于网站开发,也可以用在各种基于Bootstrap框架的Web应用中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值