【发布时间】:2014-02-19 11:24:14
【问题描述】:
我正在尝试编写一个接受 URL 并从该 URL 位置下载 WSDL 文件的 java 程序。将该 WSDL 文件以 .xml 扩展名保存在我的本地系统中。
我在 Internet 上找到了许多程序,但它们都不起作用。他们显示错误消息Access denied contact Your Service Provider.
我需要在不使用任何 IDE(Netbeans、Eclipse)的情况下下载文件。
谁能帮帮我?提前致谢。
我的 JAVA 代码
import java.awt.FlowLayout;//Defines the layout
import java.io.*;//For input-output operations
import java.net.HttpURLConnection;//For making a connection
import java.net.URL;//Helps in making a URL object
import javax.swing.JFrame;//Helps in making Swing Frame
import javax.swing.JProgressBar;//Helps in implementing Progress Bar
public class Downloader extends JFrame
{
public static final void main(String[] args) throws Exception
{
String site="http://saudishipping.net/service.asmx?wsdl";
String filename="saudi.xml";
JFrame frm=new JFrame();
JProgressBar current = new JProgressBar(0, 100);
//We are setting the size of progress bar
current.setSize(50,50);
//Initially the progress bar will be zero%.
current.setValue(0);
current.setStringPainted(true);
//Adding the progress bar to the frame
frm.add(current);
frm.setVisible(true);//Making the frame visible
frm.setLayout(new FlowLayout());
frm.setSize(400, 200);
//EXIT_ON_CLOSE make sure that the application gets exited when frame close
frm.setDefaultCloseOperation(EXIT_ON_CLOSE);
try
{
URL url=new URL(site);
HttpURLConnection connection =(HttpURLConnection) url.openConnection();
int filesize = connection.getContentLength();
float totalDataRead=0;
java.io.BufferedInputStream in = new java.io.BufferedInputStream(connection.getInputStream());
java.io.FileOutputStream fos = new java.io.FileOutputStream(filename);
java.io.BufferedOutputStream bout = new BufferedOutputStream(fos,1024);
byte[] data = new byte[1024];
int i=0;
while((i=in.read(data,0,1024))>=0)
{
totalDataRead=totalDataRead+i;
bout.write(data,0,i);
float Percent=(totalDataRead*100)/filesize;
current.setValue((int)Percent);
}
bout.close();
in.close();
}
catch(Exception e)
{
javax.swing.JOptionPane.showConfirmDialog((java.awt.Component)null,e.getMessage(), "Error",javax.swing.JOptionPane.DEFAULT_OPTION);
}
}
}
【问题讨论】:
-
向我们展示您的尝试。
-
可以通过浏览器自己访问wsdl文件吗?还是它也拒绝您访问? :P
-
是的 URL 是问题。我更正它,我可以下载文件。谢谢。