0

当用户单击按钮时,我想下载一个 pdf 文件。对于前端,我使用的是 JSF 2。实际上我想下载 pdf 文件下载图像,如 primefaces 演示中所示

pdf文件存储在我本地tomcat的根文件夹内。我以这种方式跟随,但它给了我例外

java.io.IOException:服务器返回 HTTP 响应代码:505 用于 URL:

URL url;
        URLConnection con;
        DataInputStream dis; 
        FileOutputStream fos; 
        byte[] fileData;  
        try {
            url = new URL("http://localhost:8080/html/pdf/sample.pdf"); //File download Location goes here

            System.out.println("URL "+url.toString());
            con = url.openConnection(); // open the url connection.
            dis = new DataInputStream(con.getInputStream());
            fileData = new byte[con.getContentLength()]; 
            for (int q = 0; q < fileData.length; q++) { 
                fileData[q] = dis.readByte();
            }
            dis.close(); // close the data input stream
            fos = new FileOutputStream(new File("/Users/sample.pdf")); //FILE Save Location goes here
            fos.write(fileData);  // write out the file we want to save.
            fos.close(); // close the output stream writer
        }
        catch(Exception m) {
            System.out.println(m);
        }

我是否提供了错误的路径?如果是,如图所示从本地系统 tomcat 文件夹下载文件的正确路径是什么?

4

1 回答 1

0

如果您更改了,我觉得您忘记输入port number 8080OR8052或输入您的端口号。

放置一个特定的路径。它可以工作

URL url;
URLConnection con;
DataInputStream dis; 
 FileOutputStream fos; 
 byte[] fileData;  
try {
url = new URL("http://localhost:8052/Naveed_workingfiles/a.pdf"); //File download Location goes here

System.out.println("URL "+url.toString());
con = url.openConnection(); // open the url connection.
dis = new DataInputStream(con.getInputStream());
fileData = new byte[con.getContentLength()]; 
for (int q = 0; q < fileData.length; q++) { 
    fileData[q] = dis.readByte();
}
dis.close(); // close the data input stream
fos = new FileOutputStream(new File("C:/Documents and Settings/microsoft/My Documents/Downloads/sample.pdf")); //FILE Save Location goes here
fos.write(fileData);  // write out the file we want to save.
fos.close(); // close the output stream writer
}  catch(Exception m) {
System.out.println(m);
 }
于 2013-06-17T10:21:58.543 回答