文件上傳和下載相對路徑


上傳存的是相對路徑 http://127.0.0.1/tomcat.....

public  String  upload  ()  {

File filePath = new File(ServletActionContext.getServletContext().getRealPath("/"));
      String savePath = filePath.getParentFile().getParent()+"/attachment/securitysupervision";
       File root = new File(savePath);
       if (!root.exists()) {
        //創建臨時目錄
        root.mkdirs();
        System.out.println( root.mkdir());
       }
          InputStream is = new FileInputStream(file);
          //得到上傳文件的擴展名 .xle
          String fileExtName = fileFileName.substring(fileFileName.lastIndexOf(".")+1);
      
          //得到文件的真實的保存目錄
          String realSavePath = servletRequest.getScheme() + "://"+ servletRequest.getServerName() + ":" + servletRequest.getServerPort()+servletRequest.getContextPath()+"/attachment/securitysupervision/";
          //String realSavePath = makePath(fileFileName, savePath);
          //修改文件的存儲名字,防止文件多了出現重名保存失敗
         String  storeName = makeFileName(fileFileName);
         
          //創建一個文件輸出流
          OutputStream os = new FileOutputStream(savePath + "\\" + storeName);
          byte[] buffer = new byte[1024];
          int length = 0;
          while(-1 != (length = is.read(buffer, 0, buffer.length)))
          {
            os.write(buffer);
          }
          os.close();
          is.close();
        
      //數據庫存儲的字段和名字
      String  urlAndName = realSavePath +storeName ;
      kbm.setKbmAdjunct(urlAndName);
      
         }catch (Exception e) {
      
           e.printStackTrace();
           return  "error" ;
         }

}

 

下載文件

 Kbm kbm = kbmService.selectAddrByKbmId(kbmQuery.getId()) ;
   String urlAndName = kbm.getKbmAdjunct();
  if (urlAndName != null && !urlAndName.equals("")) {
    // 處理獲取到的上傳文件的文件名的路徑部分,只保留文件名部分
    HttpServletRequest request=ServletActionContext.getRequest();
    String basePath=request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+request.getContextPath();
   System.out.println(basePath);
    String url = urlAndName.substring(basePath.length()+1, urlAndName.lastIndexOf("/"));
   String storeName = urlAndName.substring(urlAndName.lastIndexOf("/") + 1);
   String realName = storeName.substring(storeName.indexOf("_") + 1);
   // 上傳的文件都是保存在/WEB-INF/upload目錄下的子目錄當中
   //File file = new File(url + "" + storeName);
   File filePath = new File(ServletActionContext.getServletContext().getRealPath("/"));
    String savePath = filePath.getParentFile().getParent()+"/attachment/securitysupervision";
   System.out.println(savePath);
   File file = new File(savePath + "/" + storeName);
   
   // 如果文件不存在
   if (!file.exists()) {
    // 錯誤提示
    servletRequest.setAttribute("message", "您要下載的資源已被刪除!!");
    servletRequest.getRequestDispatcher("/message.jsp").forward(
      servletRequest, servletResponse);
    return "";
   }

   // 處理文件名
   // String realname =
   // fileStoreName.substring(fileStoreName.indexOf("_") + 1);
   // 設置響應頭,控制瀏覽器下載該文件
   servletResponse.setHeader(
     "content-disposition",
     "attachment;filename="
       + URLEncoder.encode(realName, "UTF-8"));
   // 讀取要下載的文件,保存到文件輸入流
   FileInputStream in = new FileInputStream(file);
   // 創建輸出流
   OutputStream out = servletResponse.getOutputStream();
   byte buffer[] = new byte[1024];
   int len = 0;
   // 循環將輸入流中的內容讀取到緩沖區當中
   while ((len = in.read(buffer)) > 0) {
    // 輸出緩沖區的內容到瀏覽器,實現文件下載
    out.write(buffer, 0, len);
   }
   // 關閉文件輸入流
   in.close();
   // 關閉輸出流
   out.close();
   
  
    return NONE;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM