public static void downloadNet() throws IOException {
// 下载网络文件
int bytesum = 0;
int byteread = 0; //URL url = new URL("windine.blogdriver.com/logo.gif");
URL url = new URL("http://levphon.vicp.cc:8081/sdps/images/404.jpg");
try {
URLConnection conn = url.openConnection();
InputStream inStream = null;
conn.getInputStream();
FileOutputStream fs = new FileOutputStream("E:/abc.jpg");
byte[] buffer = new byte[1204*100];
while ((byteread = inStream.read(buffer)) != -1) {
bytesum += byteread;
System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
我这有个程序,可以下载那种网上能在线打开并显示在网页上的文件(比如图片),但是如果把url换成那种不能显示在网页的时候就无法下载(比如给一个word文档路径,它会弹出一个下载框,不能把内容显示在网页上),请问怎么解决这问题呀。谁有新的程序也可以发我,万分感谢!!!
// 下载网络文件
int bytesum = 0;
int byteread = 0; //URL url = new URL("windine.blogdriver.com/logo.gif");
URL url = new URL("http://levphon.vicp.cc:8081/sdps/images/404.jpg");
try {
URLConnection conn = url.openConnection();
InputStream inStream = null;
conn.getInputStream();
FileOutputStream fs = new FileOutputStream("E:/abc.jpg");
byte[] buffer = new byte[1204*100];
while ((byteread = inStream.read(buffer)) != -1) {
bytesum += byteread;
System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
我这有个程序,可以下载那种网上能在线打开并显示在网页上的文件(比如图片),但是如果把url换成那种不能显示在网页的时候就无法下载(比如给一个word文档路径,它会弹出一个下载框,不能把内容显示在网页上),请问怎么解决这问题呀。谁有新的程序也可以发我,万分感谢!!!
解决方案 »
- 又是菜鸟求助
- 各位高手,笨女女这里向大家讨教了:Failed to load or instantiate TagLibraryValidator class: org.apache.taglibs.standar
- 内网服务器向外网服务器传输数据
- freemarker模板+hibernate分页问题,各位前辈帮帮忙啊
- Servlet execution threw an exception一般是由什么引起的如何解决
- byte[]转化成String(效果是string是汉字)
- 请教如何实现用JSP动态增加表单中的文本框,并切如何处理,谢谢了,很着急呀
- 请教各位一个简单问题:Hibernate 中session.load()问题
- 高分求助!!!进者有分,基于http协议的文件断点续传功能的实现
- 一个关于编译Struts架构程序的入门级问题
- 单点登录的时候出现票根'ST-xxxxxx-cas'不符合目标服务
- 求意见,多个数据库问题!
啊 , 不明白哎, 我是要从外网的服务器上下载文件 , 图片和word文件是放在同一目录下面的,图片可以下载, 但是word文档就不能下载。
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1253)
at com.gzpia.webservice.serviceAms.test.D2.downloadNet(D2.java:22)
at com.gzpia.webservice.serviceAms.test.D2.main(D2.java:39)
这是异常。
http://www.java.net/download/jdk6/6u10/promoted/b32/binaries/jdk-6u10-rc2-bin-b32-windows-i586-p-12_sep_2008.exe
在浏览器中没有显示,是个exe文件,应该与word之类的不能显示的文件类似。我这边的java工具可以下载这个文件,也可以下载你给的那个图片地址——http://levphon.vicp.cc:8081/sdps/images/404.jpg
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1182)是不是网页有链接跳转?重定向多次
urlConn.setFollowRedirects(true);
urlConn.setInstanceFollowRedirects(false);
urlConn.connect();
如果是的话,urlConn.setFollowRedirects(true);本来默认就是true
可能只需要urlConn.setInstanceFollowRedirects(false);
In all probability you have entered a redirect loop at the server. The server responds with 303 See other and when Java's URL connection implementation automatically "sees that other", the server responds again with the same response, and so on ad infinitum.It's apparently redirecting in an infinite loop because you don't maintain the user session. The session is usually backed by a cookie. You need to create a CookieManager before you use URLConnection.// First set the default cookie manager.
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));// All the following subsequent URLConnections will use the same cookie manager.
URLConnection connection = new URL(url).openConnection();
// ...connection = new URL(url).openConnection();
// ...connection = new URL(url).openConnection();
// ...看看setCookieManager的方法有没有用
urlConn.setInstanceFollowRedirects(false);这种我不知道客户端拒绝redirect有没有用,也就是服务端的redirect消息意味着什么不清楚
如果仅仅通过setInstanceFollowRedirects(false)解决的,那估计屏蔽redirect就不会影响下载
但是这个单纯的url怎么会redirect呢