文件上传路径在<a href="path" target="_blank"> element</a>中无效

时间:2013-04-25 03:58:29

标签: jsp spring-mvc file-upload file-io upload

我正在尝试使用a href =“block”显示文件。

我使用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"

请帮助我,我被困在这里几个小时。

谢谢, 惊鼓

1 个答案:

答案 0 :(得分:0)

假设fileupload文件夹位于您的网络应用程序的根目录中(例如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);
相关问题