一、技术概述
- 1 这个技术是做什么?
基于bootstrap-fileinput实现文件上传,下载
- 2 学习该技术的原因?
框架对于前段来说是很重要的一部分。
- 3 技术的难点在哪里?
如何把前段和后台联系起来。
二、技术详述
具体实现方式
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;
Iteratoriter = 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();
}
}
三、技术使用中遇到的问题和解决过程
- 1 前后端如何交互
前端部分,在前端jsp页面设置form表单,确定需要传递的参数name让用户输入,通过点击按钮后submit()提交到后台;后台对前端请求的反应,接收数据,处理数据以及返回数据。
四、总结
结对第二次作业其实也能运用这个技术实现,不过当时框架也没接触过,看上去很难的样子所以选了别的方法,实际使用过才发现这样做不仅更简单、而且效果也比我之前使用的那种更好。
所以软件开发行业中时刻掌握新技术很重要,即使对于已掌握的更熟悉,但是新的、得到广泛认可的技术真的往往能带来很大的便利。