[英]Java web service to transfer file to Local system
我想用兩種方法在java中創建一個Web服務
1)通過返回本地URL將文件從Internet傳輸到本地文件服務器
2)通過獲取url從同一服務器檢索文件
注意:它應該適用於所有格式
它必須使用Java Web服務..
任何類型:字節數組,十六進制或MIME類型傳輸都可以
附件的大小是4mb ..
我無法直接連接到數據庫,因為應用程序部署在DMZ上,我可以通過Web服務連接到Intranet中文件服務器的唯一方法。
已完成與文件服務器的連接..
既然你用soap
標記了這個問題,我將假設你想要一個Java的SOAP Web服務。 這也使JAX-WS (XML Web Services的Java API)成為庫使用的自然選擇。 Java(TM)Web服務教程將更詳細地介紹您的問題。
現在,您將需要實現邏輯以獲取圖像並返回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;
}
}
您還可能需要設置一些其他配置,如部署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>
並且還將一些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>
最后,將所有內容打包到.war文件中並將其部署到Java Web服務器(例如Tomcat)。
類似的情況: 是這個 ,他已經解釋了使用短代碼
如果您的主要問題是在java中找到通過Web服務輕松進行文件傳輸的技巧,我建議使用Hessian服務,在Hessian上討論SO上的大型二進制數據(java)帖子。 這里的鏈接是一個實現一種文件傳輸的例子。
如果您不想過多地了解Web服務本身的邏輯,那么Hessian是一個很好的解決方案。 快速查看一個Hessian代碼,你甚至不會認識到你正在使用它。 它是如此輕量級的解決方案。
Stefan有一個解決方案,您可以在Web服務邏輯中獲得相當多的內容,因此您需要具備多高的抽象級別。 如果這項任務的目的是展示如何使用Web服務,而不只是讓它工作,那么Stefan就有了答案。
關於文件上傳等,您想從Internet保存文件。 看看: 如何使用Java從Internet下載和保存文件? 。 這使用純Java,在我的理解中,不需要任何Web服務來完成給定的任務,但是如果你將這兩者結合起來,你會得到一些非常容易的東西!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.