2013-04-25 37 views
0

中不起作用我試圖使用a href =「block」顯示文件。文件上傳路徑在<a href="path" target="_blank">元素

我正在使用Spring MVC上傳文件。

以下是我對HTML上傳代碼: -

<form method="POST" action ="uploadDocument.htm" enctype="multipart/form-data"> 
    Please select your resume to upload : (.pdf, .doc , .docx)<br/> 
    <input type="file" name="fileUpload" size="50" required/><br/><br/> 
    <input type="submit" value="Upload"/>  
    </form> 

以下是控制器代碼: -

@RequestMapping(value="/uploadDocument.htm", method=RequestMethod.POST) 
    public String uploadFile(@RequestParam CommonsMultipartFile fileUpload,Model model,HttpServletRequest request) throws Exception{ 


    HttpSession session = request.getSession(); 

    UserAccount userAccount = (UserAccount)session.getAttribute("user"); 


     String contentType = fileUpload.getContentType(); 

     String format=""; 
     if(contentType.contains("pdf") || contentType.contains("vnd") || contentType.contains("msword")){ 

      if(contentType.contains("pdf")){ 
       format=".pdf"; 

      } 
      else if(contentType.contains("vnd")){ 
       format=".docx"; 

      } 
      else if(contentType.contains("msword")){ 
       format = ".doc"; 
      } 

      String fileName = userAccount.getUsername()+format; 

      System.out.println(fileName); 

      String path ="E:\\eclipse\\workspace\\Zingo_Project\\src\\main\\webapp\\fileupload\\"; 


      fileUpload.transferTo(new File(path+fileName)); 


      model.addAttribute(fileName); 
      return "fileUploadSuccessfull";    

     }   
     return "uploadUserFailure";    
    } 

我可以看到在需要的位置上載的文件。 當我嘗試訪問fileUploadSuccessfull.jsp中的文件時。

以下是我的代碼fileUploadSuccessfull.jsp。

File upload Successfull!<br/> 
</br> 

<a href="E:\New version\eclipse\workspace\Zingo_Project\src\main\webapp\fileupload\zingo.pdf" target="_blank">Click here to View the File</a> 

當我點擊鏈接時,我無法在新窗口中查看該文件。

我已經困在這裏了幾個小時,我試圖給

href as "file:///E:/New%20version/eclipse/workspace/Zingo_Project/src/main/webapp/fileupload/zingo.pdf" 
or 
"${pageContext.request.contextPath}/fileupload/zingo.pdf" 

請幫助我,我一直在這裏堅持了幾個小時。

感謝, 驚鼓

回答

0

比方說,fileupload文件夾在你的web應用程序(如www.yoursite.com/project/fileupload),爲什麼不只是有一個靜態的HREF的根源在哪裏?

<a href="/project/fileupload/zingo.pdf" target="_blank">Click here to View the File</a> 

確保您的文件上傳到正確的目錄:

String uploadDir = "/fileupload/"; // or "/~project/fileupload/" 
String path = getServletContext().getRealPath(uploadDir); 
+0

HTTP狀態404 - /fileupload/zingo.pdf 這是我得到 錯誤和URL說http:// localhost:8080/fileupload/zingo.pdf – quickBongo 2013-04-25 04:23:37

+0

1.確定你的應用沒有像localhost:8080/webapp/fileupload/zingo.pdf這樣的子文件夾? 2.確定pdf在這個文件夾中?你可以在你的操作系統中打開它嗎? – 2013-04-25 04:32:04

+0

webapp /下存在子文件夾「fileupload」,並且該文件也存在,我可以在我的操作系統中在所需文件夾中打開它。 – quickBongo 2013-04-25 04:36:03