文件下载地址动态生成,java无法下载文件
我在浏览器上面输入: http://***/.view.aspx?ID=*** 就可以直接打开一个pdf文件文件下载地址动态生成
但是用下面的方法,同样的网址,无法弹开下载界面
String formatsStr = "文件下载地址";
response.setContentType("application/octet-stream;charset=utf-8");
response.setContentLength(formatsStr.getBytes().length);
response.setHeader("Content-disposition", "attachment;filename=1.pdf");
Writer out = response.getWriter();
out.write(formatsStr);
out.flush();
out.close();
象这种动态生成pdf下载地址的页面,可以用java弹出下载框吗?
getResponseHeader("Content-Type").getValue() 返回的只是text/html格式,谢谢大家! --------------------编程问答-------------------- 改为response.setContentType("application/pdf"); --------------------编程问答-------------------- 试了,下载的pdf不是最终的pdf,其实只是一个html文件 --------------------编程问答-------------------- 你没有往输出流里写任何文件数据...你指望他给你打开啥子... --------------------编程问答-------------------- String formatsStr = "文件下载地址";
response.setContentType("application/octet-stream;charset=utf-8");
response.setContentLength(formatsStr.getBytes().length);
response.setHeader("Content-disposition", "attachment;filename=1.pdf");
Writer out = response.getWriter();
out.write(formatsStr); //这样能得到 文件下载地址 文件数据?
out.flush();
out.close(); --------------------编程问答-------------------- 可能单单这些还不足以下载pdf文件,看这个对不对
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ConnectException;
import java.net.URL;
import java.net.URLConnection;
import com.gnostice.pdfone.PdfDocument;
public class Read_PDF_From_URL {
public static void main(String[] args) throws IOException {
URL url1 =
new URL("http://www.gnostice.com/downloads/Gnostice_PathQuest.pdf");
byte[] ba1 = new byte[1024];
int baLength;
FileOutputStream fos1 = new FileOutputStream("download.pdf");
try {
// Contacting the URL
System.out.print("Connecting to " + url1.toString() + " ... ");
URLConnection urlConn = url1.openConnection();
// Checking whether the URL contains a PDF
if (!urlConn.getContentType().equalsIgnoreCase("application/pdf")) {
System.out.println("FAILED.\n[Sorry. This is not a PDF.]");
} else {
try {
// Read the PDF from the URL and save to a local file
InputStream is1 = url1.openStream();
while ((baLength = is1.read(ba1)) != -1) {
fos1.write(ba1, 0, baLength);
}
fos1.flush();
fos1.close();
is1.close();
// Load the PDF document and display its page count
System.out.print("DONE.\nProcessing the PDF ... ");
PdfDocument doc = new PdfDocument();
try {
doc.load("download.pdf");
System.out.println("DONE.\nNumber of pages in the PDF is " +
doc.getPageCount());
doc.close();
} catch (Exception e) {
System.out.println("FAILED.\n[" + e.getMessage() + "]");
}
} catch (ConnectException ce) {
System.out.println("FAILED.\n[" + ce.getMessage() + "]\n");
}
}
} catch (NullPointerException npe) {
System.out.println("FAILED.\n[" + npe.getMessage() + "]\n");
}
}
}
http://www.gnostice.com/nl_article.asp?id=207&t=How_To_Read_A_PDF_File_From_A_URL_In_Java --------------------编程问答-------------------- 只是弹出下载框啊,现在问题不在这,而是这个pdf文件地址,是跳转以后的,现在只能下载了一个txt/html文件 --------------------编程问答-------------------- 非动态地址的pdf文件,已经可以弹出下载框,现在是动态地址下载问题, 迅雷可以解决这个问题,不知道在程序里面如何实现 --------------------编程问答--------------------
你说的动态和静态区别在哪呢,对于下载来说没啥区别吧? --------------------编程问答-------------------- window.open(下载文件地址)
补充:Java , Web 开发