国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

表單文件上傳與文件下載

2019-11-15 00:02:36
字體:
來源:轉載
供稿:網友
表單文件上傳與文件下載

一、簡介

  使用form表單進行需要為form添加enctype="mult

二、示例

  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="導&nbsp;入" />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 = "

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 彩票| 洱源县| 双鸭山市| 太湖县| 潜江市| 永州市| 明水县| 北川| 沁阳市| 沅江市| 都匀市| 吉木萨尔县| 霍州市| 拜泉县| 习水县| 衡水市| 嘉鱼县| 朝阳市| 新乡市| 太和县| 陆良县| 吉隆县| 宁波市| 辉县市| 中牟县| 通山县| 沙坪坝区| 乐陵市| 精河县| 马边| 天等县| 赤壁市| 扶绥县| 黄平县| 磴口县| 离岛区| 监利县| 苍溪县| 赣州市| 桃园市| 新宾|