【问题标题】:Servelt any file upload using contextPath and servletContext upload successfully download can't DownloadServlet 使用 contextPath 和 servletContext 上传任何文件上传成功下载无法下载
【发布时间】:2017-07-16 06:46:51
【问题描述】:

我完成了文件上传:

                 ServletContext servletContext = getServletContext();
         String contextPath = servletContext.getRealPath(File.separator);

         String path = contextPath + "\\uploads\\" + session.getAttribute("seusername");
         System.out.println(path);

         File file=new File(path);
         if(!file.exists())
             file.mkdirs();
         Part filePart = request.getPart("uploadfile");
         //return content type of the file
         String type = filePart.getHeader("content-type");

         //checking content type of file. 
         if (!type.equals("application/x-msdownload")) {

             final String fileName = getFileName(filePart);
             myfilename=fileName;
            try {
                 EncriptFilename= aESCryp.encrypt(fileName);
                System.out.println(EncriptFilename);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


             OutputStream fout = null;
             InputStream filecontent = null;

             try {
                 fout = new FileOutputStream(new File(path + File.separator + fileName));
                 filecontent = filePart.getInputStream();
                 int read = 0;
                 final byte[] bytes = new byte[32 * 1024];

                 while ((read = filecontent.read(bytes)) != -1) {
                     fout.write(bytes, 0, read);
                 }

                 fout.flush();
                 fout.close();
             } catch (Exception er) {
                 System.out.println("error:"+er.getMessage());
             }
         }

我上传了图片、pdf、doc文件,,,很好.. 在我的本地光盘文件夹上的文件位置之后。 D:\JavaWorkspace.metadata.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\File\uploads\user\java_coffee_cup_logo1600.png

我的问题是......如何下载这个文件,,, 我不能用href链接下载..

【问题讨论】:

    标签: javascript java servlets


    【解决方案1】:

    您的网络应用可以通过与您帖子中的Servlet 做相反的事情来支持文件下载。

    创建“下载”Servlet,并在您的web.xml 中配置到Servlet 的映射(或使用注释来定义映射)。此 servlet 的 URL 可能如下所示: http://machine.com/my-app/download/my-file.jpg

    在下载Servlet 中,查看请求URL 以发现请求的文件名(my-file.jpg),然后使用FileInputStream 打开并读取my-file.jpgrequest.getPathInfo() 可能会为您提供确定用户想要下载的文件所需的信息。见javadoc for HttpServletRequest.getPathInfo().

    请注意,my-file.jpg 可以存在于您想要的任何位置。您的 Servlet 可以将请求 URL 中的文件名和路径映射到本地文件系统上的任意位置。该文件甚至可以存在于另一个 Web 服务器上。您只需要能够创建一个访问该文件的InputStream

    使用该文件的路径信息,创建一个FileInputStream 来访问该文件。然后将FileInputStream 复制到ServletResponse 的输出流中。这个SO post 和这个SO post 给出了如何将InputStream 复制到OutputStream 的示例。

    您可以像这样获取响应的输出流:response.getOutputStream()。完成后不要忘记关闭InputStreamOutputStream

    【讨论】:

      猜你喜欢
      • 2011-10-01
      • 2015-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多