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

首頁 > 編程 > Java > 正文

java web上傳文件和下載文件代碼片段分享

2019-11-26 13:11:24
字體:
供稿:網(wǎng)友

本文實例為大家分享了java web上傳文件和下載文件的具體代碼,供大家參考,具體內(nèi)容如下

 /**  * Purpose:上傳文檔  *   * @param req  * @param fileTitle  * @param fileType  * @param fileDesc  * @return  */ @RequestMapping("upload") public ModelAndView upload(HttpServletRequest req, String fileType, String fileDesc, String share) {  UserAllInfo userAll = (UserAllInfo) req.getSession().getAttribute("userAll");  ModelAndView mav = new ModelAndView();  // 獲取文件的大小  String fileSize = "";  int length = req.getContentLength();  //轉(zhuǎn)換成KB  double len1 = (double) (Math.round((length / 1024) * 100)) / 100;  //轉(zhuǎn)換成MB  double len2 = (double) (Math.round((len1 / 1024) * 100)) / 100;  if (len2 > 1) {   fileSize = String.valueOf(len2) + "MB";  } else {   fileSize = String.valueOf(len1) + "KB";  }  //將請求轉(zhuǎn)換成處理文件的請求  MultipartRequest mreq = (MultipartRequest) req;  //文件上傳專用類  MultipartFile mfile = mreq.getFile("uploadFile");  // 得到上下文路徑  String root = req.getSession().getServletContext().getRealPath("/");  File dir = new File(root);  // 獲取文件名和文件的mime類型  String str = mfile.getOriginalFilename();  String[] st = str.split("http://.");  File savedFile = null;  try {   //在指定目錄中創(chuàng)建一個新的空文件,文件名稱加前綴"upload_"   savedFile = File.createTempFile("upload_", mfile.getOriginalFilename(), dir);   //將緩沖區(qū)的內(nèi)容復(fù)制到新創(chuàng)建的文件中   FileCopyUtils.copy(mfile.getInputStream(), new FileOutputStream(savedFile));  } catch (Exception e) {   e.printStackTrace();  }  String path = savedFile.getPath();  // 將文件的信息封裝在對象中  FileInfo file = new FileInfo();  if (userAll != null) {   file.setUploadPersonId(userAll.getUser().getUserId());  } else {   mav.setViewName("login");   return mav;  }  file.setFileName(st[0]);  file.setFilePath(path);  Date date = new Date();  file.setUploadDate(date);  file.setFileSize(fileSize);  file.setFileType(fileType);  file.setFileForm(st[1]);  if ("superman".equals(userAll.getUser().getUserNumber())) {   file.setFileShare("Y");   file.setCheckFlag("Y");  } else {   file.setFileShare(share);   file.setCheckFlag("N");  }  file.setDeleteFlag("N");  file.setDownloadTimes(0);  // 保存文件的信息  boolean bool = fileSer.saveFile(file);  String userNumber = userAll.getUser().getUserNumber();  //設(shè)置跳轉(zhuǎn)界面  mav.setViewName("jsp/person/upload");  logSer.saveLog("用戶/"" + userNumber + "/"上傳文件/"" + file.getFileName() + "/"");  mav.getModel().put("bool", bool);  return mav; } /**  * Purpose:下載文檔  *   * @param fileId 文件id  * @return  */ @RequestMapping("download") public void download(Integer fileId, HttpServletResponse resp) {  FileInfo file = new FileInfo();  //通過文件id獲取文件的基本信息  file = fileSer.getFileById(fileId);  // 根據(jù)文件后綴名設(shè)置文件的mime類型  String mime = "application/";  switch (file.getFileForm()) {  case "doc":   mime = mime + "msword";   break;  case "docx":   mime = mime + "msword";   break;  case "pdf":   mime = mime + "pdf";   break;  case "xls":   mime = mime + "vnd.ms-excel";  case "ppt":   mime = mime + "vnd.ms-powerpoint";   break;  case "txt":   mime = "text/plain";   break;  default:   break;  }  resp.setContentType(mime);  String fileName = file.getFileName();  String name = "";  try {   // 將文件名使用utf-8編碼格式編碼,使其中的中文字符可以正常顯示   name = URLEncoder.encode(fileName, "utf-8");  } catch (UnsupportedEncodingException e1) {   e1.printStackTrace();  }  // 彈框選擇保存路徑  resp.setHeader("content-disposition", "attachment;filename=" + name + "." + file.getFileForm()    + ";filename*=utf-8''" + name + "." + file.getFileForm());  String filePath = file.getFilePath();  InputStream is = null;  OutputStream os = null;  try {   is = new BufferedInputStream(new FileInputStream(filePath));   os = new BufferedOutputStream(resp.getOutputStream());   byte[] buff = new byte[1024];   @SuppressWarnings("unused")   int count;   while ((count = is.read(buff)) != -1) {    os.write(buff);   }   //修改下載次數(shù)   fileSer.update(fileId);  } catch (Exception e) {   e.printStackTrace();  } finally {   //最后,要記得關(guān)閉輸入流和輸出流   if (is != null) {    try {     is.close();    } catch (IOException e) {     e.printStackTrace();    }   }   if (os != null) {    try {     os.close();    } catch (IOException e) {     e.printStackTrace();    }   }  } }

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 筠连县| 米泉市| 新昌县| 洛南县| 隆安县| 闵行区| 芦山县| 铁力市| 迁西县| 永顺县| 清水河县| 巴塘县| 六安市| 砚山县| 孝义市| 方城县| 平潭县| 东平县| 从江县| 岗巴县| 邓州市| 包头市| 岑巩县| 东源县| 孙吴县| 抚宁县| 石门县| 阜宁县| 历史| 汉寿县| 海晏县| 永春县| 若尔盖县| 海安县| 阳朔县| 盐亭县| 新兴县| 全南县| 闵行区| 扶绥县| 沅陵县|