폼 파일 업로드 및 파일 다운로드
15687 단어 파일 업로드
form 폼 을 사용 하여 진행 하려 면 form 에 enctype="multipart/form-data"속성 을 추가 해 야 합 니 다.이외에 폼 의 제출 방법 을 post 로 변경 해 야 합 니 다.다음 method="post"입 니 다.
예시
1.폼 파일 업로드
웹 페이지 코드 는 다음 과 같 습 니 다.
1 <!DOCTYPE html>
2 <html>
3 <head>
4 </head>
5 <body>
6 <form action="${pageContext.request.contextPath}/file/upload.action" method="post" enctype="multipart/form-data">
7 <div id="contentTable" style="border: 0px;">
8 <h1 class="title" style="font-size: 15px; border-bottom: 1px solid #DFE3E6;"> </h1>
9 <table width="80%">
10 <tr>
11 <td width="20%"align="right">
12
13 </td>
14 <td width="70%" id="name_h" title="" style="text-align: center;">
15 <input type="file" name="xlsfile" id="xlsfile" />
16 </td>
17 </tr>
18 </table>
19 <div id="activityTable">
20 <input id="btnSave" type="submit" value=" " />
21 </div>
22 </div>
23 </form>
24 </body>
25 </html>
백 엔 드 업로드 처리 코드:
1 /**
2 * springmvc
3 */
4 @RequestMapping("upload")
5 @ResponseBody
6 public boolean upload(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws UnsupportedEncodingException {
7 String path = request.getSession().getServletContext().getRealPath("");
8 Calendar calendar = Calendar.getInstance();
9 calendar.setTime(new Date());
10 request.setCharacterEncoding("UTF-8");
11 path = String.format("%s\\%s\\%s\\%s\\%s\\%s", path, "upload", "file", calendar.get(calendar.YEAR),
12 calendar.get(calendar.MONTH), calendar.get(calendar.DAY_OF_MONTH));
13 File filepath = new File(path);
14 if (!filepath.exists()) {
15 filepath.mkdirs();
16 }
17 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
18
19 MultipartFile multipartFile = multipartRequest.getFile("xlsfile");
20 OutputStream os = null;
21 InputStream is = null;
22 File uploadFile = null;
23 try {
24 is = multipartFile.getInputStream();
25 uploadFile = new File(filepath, System.currentTimeMillis() + ".xls");
26 os = new FileOutputStream(uploadFile);
27 IOUtils.copy(is, os);// commons-io
28 os.flush();
29 } catch (IOException e) {
30 e.printStackTrace();
31 return false;
32 }finally{
33 IOUtils.closeQuietly(os);
34 IOUtils.closeQuietly(is);
35 }
36 return true;
37 }
2.파일 다운로드
1 /**
2 * springmvc
3 */
4 @RequestMapping({ "/template" })
5 public void downloadTemplate(HttpServletRequest request, HttpServletResponse response)
6 throws UnsupportedEncodingException {
7 String path = request.getSession().getServletContext().getRealPath("");
8 String filename = " .xls";
9 File file = new File(path + "\\file\\templagte\\" + filename);
10 String userAgent = request.getHeader("User-Agent");
11 byte[] bytes = userAgent.contains("MSIE") ? filename.getBytes() : filename.getBytes("UTF-8"); // fileName.getBytes("UTF-8") safari
12 String fileName = new String(bytes, "ISO-8859-1");
13 //
14 response.setContentType("multipart/form-data");
15 response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);
16
17 InputStream inStream = null;
18 try {
19 inStream = new FileInputStream(file);
20 IOUtils.copy(inStream, response.getOutputStream());// commons-io
21 } catch (IOException e) {
22 e.printStackTrace();
23 }finally{
24 IOUtils.closeQuietly(inStream);
25 }
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
【Laravel】 CSV 파일 업로드의 검증이 통과되지 않은 원인화면에서 CSV아일의 업로드를 하는 기능을 만들게 되어, 아무것도 없는 채로 구현했으므로, 조금 실행해 볼까와 동작 확인한 바, 밸리데이션이 통과하지 않는다. 가볍게 빠져서 해결까지의 흐름을 적어 둡니다. 이번에는별...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.