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








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 getRepeatDataGroup(List list) {
		List result = new ArrayList<>();
		Map> map = new HashMap>();
		for (int i = 0; i < list.size(); i++) {
			if (map.containsKey(list.get(i))) {
				List lis = map.get(list.get(i));
				lis.add(i + 2);
				map.put(list.get(i), lis);
			} else {
				List lis = new ArrayList();
				lis.add(i + 2);
				map.put(list.get(i), lis);
			}
		}

		if (map.size() < list.size()) {
			for (Map.Entry> 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 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();
			}
		}
	}

 

你可能感兴趣的:(spring,mvc)