2014-02-21 80 views
0

我有兩個servlet的Java應用程序。第一個servlet將上傳文件。第二個是檢索上傳的文件。從一個servlet獲取上傳的文件屬性到其他servlet

我可以上傳文件並將其轉發到第二個servlet。但是,當我試圖檢索得到空指針異常的th文件。如何檢索第二個servlet中的文件屬性。

我曾嘗試從SERV1轉發到serv2上檢索上傳的文件

Serv1.java:

private static final String UPLOAD_DIR = "../AISNMEA/WebContent/WEB-INF/resources"; 

public String file1; 
String filePath; 
String uploadFilePath; 
UploadFile upfile; 
String fileName; 
public void doPost(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException { 
    try { 
     // set location for saving uploaded file 
     UploadBean upb = new UploadBean(); 

     upb.setFolderstore(UPLOAD_DIR); 
     upb.setFilesizelimit(1073741824); 
     upb.setOverwrite(true); 

     MultipartFormDataRequest nreq = new MultipartFormDataRequest(
       request); 
     // completed file uploading 
     upb.store(nreq); 
     Hashtable<?, ?> ht = nreq.getFiles();// gives the uploaded file 
     Enumeration<?> e = ht.elements(); 
     while (e.hasMoreElements()) { 
      upfile = (UploadFile) e.nextElement(); 
      uploadFilePath = UPLOAD_DIR + File.separator; 
      File fileSaveDir = new File(uploadFilePath); 
      if (!fileSaveDir.exists()) { 
       fileSaveDir.mkdirs(); 
      } 
      file1 = UPLOAD_DIR + File.separator + upfile.getFileName(); 
      filePath = new File(file1).getAbsolutePath(); 
      fileName = upfile.getFileName() 
        .substring(0, upfile.getFileName().indexOf(".")) 
        .replaceAll("\\s+", "").trim().concat("_") 
        .concat("data"); 
     }    
     ServletContext sc = getServletContext(); 
     HttpSession session=request.getSession(); 
     session.setAttribute("fileName", filePath); 
     RequestDispatcher rd = sc.getRequestDispatcher("/vdr.html"); 
     rd.forward(request, response); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

Serv2.java:

public void doPost(HttpServletRequest req, HttpServletResponse res) 
     throws ServletException, IOException { 
    res.setContentType("text/html"); 

    double base_x = Double.parseDouble(req.getParameter("n1")); 
    double base_y = Double.parseDouble(req.getParameter("n2")); 
    double m0 = Double.parseDouble(req.getParameter("n3")); 
    double a = Double.parseDouble(req.getParameter("n4")); 
    double b = Double.parseDouble(req.getParameter("n5")); 
    double k1 = Double.parseDouble(req.getParameter("n6")); 
    double k2 = Double.parseDouble(req.getParameter("n7")); 
    double k3 = Double.parseDouble(req.getParameter("n8")); 
    double k4 = Double.parseDouble(req.getParameter("n9")); 
    double k5 = Double.parseDouble(req.getParameter("n10")); 
    double k6 = Double.parseDouble(req.getParameter("n11")); 
    double k7 = Double.parseDouble(req.getParameter("n12")); 
    double k8 = Double.parseDouble(req.getParameter("n13")); 
    double Lbp = Double.parseDouble(req.getParameter("n14")); 

    double ido00 = base_x * PI/180.0; 

    double keido00 = base_y * PI/180.0; 

    double s0 = k1 * ido00 + k2 * Math.sin(2.0 * ido00) + k3 
      * Math.sin(4.0 * ido00) + k4 * Math.sin(6.0 * ido00) + k5 
      * Math.sin(8.0 * ido00) + k6 * Math.sin(10.0 * ido00) + k7 
      * Math.sin(12.0 * ido00) + k8 * Math.sin(14.0 * ido00); 
    double f = 1.0/299.152813; 
    double e2 = (Math.pow(a, 2) - Math.pow(b, 2))/Math.pow(a, 2); 
    HttpSession session=req.getSession(); 
    filePath = (String) session.getAttribute("fileName"); 
    System.out.println("File Path" + filePath); 
    doGet(req, res); 
} 

的index.html:

<form action="ser1" method="post" enctype="multipart/form-data" onsubmit="return Validate(this);"> 
<input name="fileName" type="file"/> 
</form> 

vdr.html:

<form name="myForm" action="ser2" method="post" 
    onsubmit="return Validate(this);"> 
</form> 

由於提前

+0

你可以把會議範圍,並試圖從sessionscope?和request.setAttribute(「fileName」,「file」); 「文件」是String.you需要把我認爲的文件參考。 – Naren

+0

您是否定義了一個空的全局請求對象?你的函數接受一個名爲'request'的參數,但你試圖從一個名爲'req'的變量中讀取屬性。 – developerwjk

+0

@developerwjk是的,我已經定義爲HttpServletRequest httpreq = null;在Serv2.class中並稱爲String filePath =(String)httpreq.getAttribute(「fileName」);但獲得相同的空指針異常。 – spt

回答

2

要保存的file name in request scope,所以它只能在vdr.html可用,而不是在Serv2.java

如果保存file name in session scope,文件名將在session之外。

你必須要做到這一點像,

從,

request.setAttribute("fileName", "file"); 

要,

session.setAttribute("fileName", "file"); 

然後你就可以得到的文件名,

String filePath = (String) session.getAttribute("fileName"); 

還有一個附加信息,

Servlets不是線程安全的,所以請嘗試聲明doGet()和doPost()中的變量,這是一個好習慣。

希望它有幫助。

+0

我有一個web應用程序來上傳包含1000個文件的zip文件並生成一個可下載的excel文件。當我閱讀包含超過1000個文件的zip文件時出現「內存不足錯誤」。如何擺脫它。 – spt

相關問題