简体   繁体   English

使用contextPath和servlet上载任何文件上传成功下载后无法下载

[英]Servelt any file upload using contextPath and servletContext upload successfully download can't Download

i done file uploading using : 我使用完成了文件上传:

                 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());
             }
         }

I am uploaded image,pdf,doc files ,,,its is fine.. after the file location on my local disc folder. 我在本地光盘文件夹中的文件位置后上传了图片,pdf,doc文件...。 D:\\JavaWorkspace.metadata.plugins\\org.eclipse.wst.server.core\\tmp1\\wtpwebapps\\File\\uploads\\user\\java_coffee_cup_logo1600.png d:\\ JavaWorkspace.metadata.plugins \\ org.eclipse.wst.server.core \\ TMP1 \\ wtpwebapps \\文件\\上传\\用户\\ java_coffee_cup_logo1600.png

my question is ...how to download this file ,,, i can't download with href link.. 我的问题是...如何下载此文件,我无法通过href链接进行下载。

Your web app can support file downloads by essentially doing the opposite of what the Servlet in your post is doing. 您的Web应用程序可以通过执行与帖子中Servlet相反的方式来支持文件下载。

Create a "Download" Servlet , and configure a mapping to the Servlet in your web.xml (or use annotations to define the mapping). 创建一个“下载” Servlet ,并在您的web.xml配置到Servlet的映射(或使用批注定义映射)。 The URL to this servlet could look like: http://machine.com/my-app/download/my-file.jpg 该servlet的URL可能类似于: http : //machine.com/my-app/download/my-file.jpg

In the Download Servlet , look at the request URL to discover the requested file name ( my-file.jpg ), and then use a FileInputStream to open and read my-file.jpg . 在下载Servlet ,查看请求URL以发现请求的文件名( my-file.jpg ),然后使用FileInputStream打开并读取my-file.jpg request.getPathInfo() will likely give you the information you need to determine the file the user wants to download. request.getPathInfo()可能会为您提供确定用户要下载的文件所需的信息。 See the javadoc for HttpServletRequest.getPathInfo() . 有关HttpServletRequest.getPathInfo()请参见javadoc。

Note that my-file.jpg can exist anywhere you want. 请注意, my-file.jpg可以存在于任何位置。 Your Servlet can map the file name and path in the request URL to an arbitrary place on your local file system. 您的Servlet可以将请求URL中的文件名和路径映射到本地文件系统上的任意位置。 The file could even exist on another web server. 该文件甚至可能存在于另一个Web服务器上。 You just need to be able to create an InputStream that accesses the file. 您只需要能够创建一个访问文件的InputStream

Using this path information about the file, create a FileInputStream to access the file. 使用关于文件的此路径信息,创建FileInputStream来访问文件。 Then copy the FileInputStream to the ServletResponse 's output stream. 然后将FileInputStream复制到ServletResponse的输出流。 This SO post and this SO post give examples how to do copy an InputStream to an OutputStream . SO帖子和本SO帖子提供了如何将InputStream复制到OutputStream示例。

You can get the response's output stream like this: response.getOutputStream() . 您可以像这样获取响应的输出流: response.getOutputStream() Don't forget to close the InputStream and OutputStream when you are done. 完成后,不要忘记关闭InputStreamOutputStream

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM