一、技術概述
- 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()提交到后台;后台對前端請求的反應,接收數據,處理數據以及返回數據。
四、總結
結對第二次作業其實也能運用這個技術實現,不過當時框架也沒接觸過,看上去很難的樣子所以選了別的方法,實際使用過才發現這樣做不僅更簡單、而且效果也比我之前使用的那種更好。
所以軟件開發行業中時刻掌握新技術很重要,即使對於已掌握的更熟悉,但是新的、得到廣泛認可的技術真的往往能帶來很大的便利。