Java Web 服务将文件传输到本地系统
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7810746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java web service to transfer file to Local system
提问by Praneel PIDIKITI
I want to create a web service in java with two methods
我想用两种方法在java中创建一个Web服务
1) to transfer a file from the internet to a local file server by returning local URL
1) 通过返回本地 URL 将文件从 Internet 传输到本地文件服务器
2) to retrieve the file from the same server by taking the url
2) 通过获取 url 从同一服务器检索文件
Note: It should work with all the formats
注意:它应该适用于所有格式
Its mandatory to use Java Web service..
必须使用 Java Web 服务。
any Type : Byte Array , Hexadecimal or MIME Type Transfer is OK
任何类型:字节数组、十六进制或 MIME 类型传输都可以
The size of the attachment is 4mb..
附件大小为4mb..
I can not connect to database directly because the application is deployed on DMZ and the only way I can connect to the file server in Intranet is by using Webservices.
我无法直接连接到数据库,因为该应用程序部署在 DMZ 上,我可以连接到 Intranet 中的文件服务器的唯一方法是使用 Webservices。
Connection to the fileserver is already done..
与文件服务器的连接已经完成..
回答by Hyman Edmonds
Since you've tagged this question with soap
, I'm going to assume you want a SOAP web service in Java. This also makes JAX-WS(the Java API for XML Web Services) a natural choice for the library to use. The Java(TM) Web Services Tutorialwill cover your problem in greater detail.
由于您已经用 标记了这个问题soap
,我将假设您需要 Java 中的 SOAP Web 服务。这也使得JAX-WS(用于 XML Web 服务的 Java API)成为库使用的自然选择。在的Java(TM)Web服务教程将介绍更详细的问题。
Now you're going to need to implement the logic to take images and return URLs, and take URLs and return images.
现在您将需要实现获取图像并返回 URL 以及获取 URL 并返回图像的逻辑。
@WebService
public class MyJavaWebService {
@WebMethod
public String takeImage(byte[] image, String imageName) {
//You'll need to write a method to save the image to the server.
//How you actually implement this method is up to you. You could
//just open a FileOutputStream and write the image to a file on your
//local server.
this.saveImage(image, imageName);
//Here is where you come up with your URL for the image.
return this.computeURLForImage(imageName);
}
@WebMethod
public byte[] getImage(String url) {
final byte[] loadedImage = this.getImage(url);
return loadedImage;
}
}
You'll also probably need to set up some additional configuration as described in Deploying Metro Endpoint. The gist of the article is that you need to add a sun-jaxws.xml
file to your WEB-INF/
folder of the form
您可能还需要设置一些额外的配置,如部署 Metro Endpoint 中所述。文章的要点是您需要将sun-jaxws.xml
文件添加到WEB-INF/
表单的文件夹中
<endpoints
xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"
version="2.0">
<endpoint
name="MyJavaWebService"
implementation="com.mycompany.MyJavaWebService"
url-pattern="/MyJavaWebService"/>
</endpoints>
And also add some JAX-WS stuff to your web.xml
file like so:
并且还将一些 JAX-WS 内容添加到您的web.xml
文件中,如下所示:
<web-app>
<listener>
<listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener>
<servlet>
<servlet-name>MyJavaWebServiceServlet</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MyJavaWebServiceServlet</servlet-name>
<url-pattern>/MyJavaWebService</url-pattern>
</servlet-mapping>
</web-app>
Finally, package everything up into a .war file and deploy it to a Java web server (e.g. Tomcat).
最后,将所有内容打包成 .war 文件并将其部署到 Java Web 服务器(例如 Tomcat)。
回答by Purushottam
回答by mico
If your main problem is to find tips for easy file transfer over a web service in java, I would recommend the Hessian service, discussed on Hessian with large binary data (java)post on SO. The link there goes for an example that implements one kind of file transfering.
如果您的主要问题是找到通过 Java 中的 Web 服务轻松传输文件的技巧,我会推荐 Hessian 服务,在Hessian 上讨论,并在 SO 上发表了大型二进制数据 (java)帖子。那里的链接是一个实现一种文件传输的例子。
Hessian is a good solution, if you don't want to mess up too much with the logic of the web services itself. Looking a Hessian code rapidly you would not even recognize you are using one. It is so light weight solution.
Hessian 是一个很好的解决方案,如果您不想过多地弄乱 Web 服务本身的逻辑。快速查看 Hessian 代码,您甚至不会意识到您正在使用它。它是如此轻量级的解决方案。
Stefan has a solution, where you get pretty much inside the Web Services logics, so it is up to you how high abstraction level you want to have. If the point of this task is to show how to use web services and not just get it work, then Stefan has the answer.
Stefan 有一个解决方案,您可以在其中深入了解 Web 服务逻辑,因此您想要拥有多高的抽象级别取决于您。如果此任务的重点是展示如何使用 Web 服务而不仅仅是让它工作,那么 Stefan 有答案。
About file upload and such, you wanted to save a file from internet. Look this: How to download and save a file from Internet using Java?. This uses pure Java and in my understanding does not need any web services to accomplish the given task, but if you combine these two you get something that works very easily!
关于文件上传等,您想从互联网上保存文件。看看这个:如何使用 Java 从 Internet 下载和保存文件?. 这使用纯 Java 并且在我的理解中不需要任何 Web 服务来完成给定的任务,但是如果将这两者结合起来,您会得到一些非常容易工作的东西!