使用Java中的Web服务(tomcat+ AXIS2),应用程序的根目录在哪里?
我创建了一个将图像下载到本地网络服务器的函数。 当我像 Java 应用程序一样运行这个函数时,它工作得很好。但是当我尝试使用 AXIS2 制作的 Web 服务运行此方法时 ( http://localhost:8080 /axis2/services/adoroCinemaService2/downloadPhoto ),AXIS2 返回内部服务器错误。
它可能会发生,因为我在代码中使用了“根路径”。那么,我需要做什么来解决这个问题呢?我的服务的根源在哪里?我该如何设置这条路径?
public void downloadPhoto() throws IOException{
URL url = new URL("http://vamosla.mobi/img/bonde.png");
String target = "vamosla.jpg";
HttpURLConnection c = (HttpURLConnection)url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
FileOutputStream f = new FileOutputStream(new File(target));
InputStream in = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ( (len1 = in.read(buffer)) > 0 ) {
f.write(buffer,0, len1);
}
f.close();
}
I create a function that downloads a image to my local web server.
When I run this function like a Java Application, it works fine. But when I try to run this method using the Web Service made by AXIS2 ( http://localhost:8080/axis2/services/adoroCinemaService2/downloadPhoto ), the AXIS2 returns Internal server error.
It happens probabily, because I use a "root path" in my code. So, what I need to do to solve this problem? Where is the root of my service? How can I setup this path?
public void downloadPhoto() throws IOException{
URL url = new URL("http://vamosla.mobi/img/bonde.png");
String target = "vamosla.jpg";
HttpURLConnection c = (HttpURLConnection)url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
FileOutputStream f = new FileOutputStream(new File(target));
InputStream in = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ( (len1 = in.read(buffer)) > 0 ) {
f.write(buffer,0, len1);
}
f.close();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,当然有一些技巧可以用来确定代码当前执行的位置并从那里设置相对路径,但我认为这对您来说并不可靠。
因此,我建议您通过系统属性或通过从类路径加载的某些配置文件来配置“asset.path”之类的内容。
Hm, there are certainly some tricks available to work out where the code currently executes and setup relative paths from there, but i don't think that will work reliably for you.
Therefore, i suggest you configure something like an 'asset.path' either via system properties or via some configuration file you load from your classpath.