2013-04-25 40 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