简体   繁体   English

如何在Java中使用WebService上传多个文件?

[英]How to upload multiple files using webservice with java?

Well, I've searched here about how to make upload files using webservice on java, but without any satisfactory answer. 好吧,我在这里搜索了有关如何使用Java上的Web服务制作上传文件的信息,但没有任何令人满意的答案。 I need to build a method where i recevie some Strings, and a list of files. 我需要建立一种方法,在其中我可以接收一些字符串和文件列表。 Someone can give me a direction about how to create that webservice where i can upload multiple files? 有人可以指导我如何创建可上传多个文件的Web服务吗?

@WebMethod()
public String criarPA(String name, List<File> files)

Its something like this... I've already seen that i cant use File... So what can i use instead of? 像这样...我已经看到我不能使用文件...那我该怎么用呢?

You cannot use File because SOAP protocol used in WebService doesn't have such type. 您不能使用File,因为WebService中使用的SOAP协议没有这种类型。 But you can always send array of bytes: 但是您始终可以发送字节数组:

@XmlType
public class SoapFile implements Serializable {

  private String fileName;
  private byte[] fileData;

  public String getFileName() {
     return fileName;
  }

  public void setFileName(String fileName) {
     this.fileName = fileName;
  }

  public byte[] getFileData() {
     return fileData;
  }

  public void setFileData(byte[] fileData) {
     this.fileData = fileData;
  }
}

And now your code will look something like this: 现在,您的代码将如下所示:

@WebMethod
public String criarPA(List<SoapFile> files)

Next you just have to create File from byte array saved in SoapFile with standard "Java" way. 接下来,您只需要使用标准的“ Java”方法从保存在SoapFile字节数组创建File


I hope it will help. 希望对您有所帮助。

Here is a way, you should send a list of byte[]. 这是一种方法,您应该发送一个byte []列表。 If you want the name of the file, you should add that attribute as well. 如果需要文件名,则还应该添加该属性。

An important thing to note if you are transferring files through Web Services in Java, you should enable MTOM, which improves performance. 如果要通过Java中的Web服务传输文件,则需要注意的重要事项是应启用MTOM,这样可以提高性能。 Here is the header of the WS endpoint implemented as a stateless EJB: 这是实现为无状态EJB的WS端点的标头:

@WebService 
@WebContext(contextRoot="FileWS")
@MTOM(enabled=true)
@Stateless
public class FileWS implements IFileWS{

    @WebMethod(operationName = "sendFiles", action = "sendFiles")
    public void sendFiles(@WebParam(name = "name")String name, 
        @WebParam(name = "files")ArrayList<byte[]> files) {

"File" is not a supported type in java web services. Java Web服务中不支持“文件”类型。

If you want to know supported types by java web services, refer to this page (section 3.2.3 Using Supported Data Types for Java Web Services) : http://docs.oracle.com/cd/B15897_01/web.1012/b14027/javaservices.htm 如果要了解Java Web服务支持的类型,请参考此页面(第3.2.3节“使用Java Web服务支持的数据类型”): http : //docs.oracle.com/cd/B15897_01/web.1012/b14027 /javaservices.htm

I suggest you implement a web service that upload just one file on server side, then on client side you call this method as much as you have files ;) 我建议您实现一个Web服务,该服务仅在服务器端上载一个文件,然后在客户端上调用该方法,只要您拥有文件即可;)

Here is a tutorial that implement a java web service for uploading a file : http://www.ibm.com/developerworks/library/ws-devaxis2part3/section2.html 这是实现用于上传文件的Java Web服务的教程: http : //www.ibm.com/developerworks/library/ws-devaxis2part3/section2.html

I hope this help :). 希望对您有所帮助:)。

Regards, 问候,

Aymen 艾门

Another way for it is 另一种方法是

You can upload the images using SAAJ. 您可以使用SAAJ上传图像。

The SAAJ API allows you to do XML messaging from the Java platform:
By simply making method calls using the SAAJ API, you can read and write
SOAP-based XML messages, and you can optionally send and receive such 
messages over the Internet (some implementations may not support sending
and receiving). 

Please check here how it works for files. 在此处检查文件的工作方式。


Creating an AttachmentPart Object and Adding Content: 创建一个AttachmentPart对象并添加内容:

AttachmentPart attachment = message.createAttachmentPart();

String stringContent = "Update address for Sunny Skies " +
    "Inc., to 10 Upbeat Street, Pleasant Grove, CA 95439";
attachment.setContent(stringContent, "text/plain");
attachment.setContentId("update_address");

message.addAttachmentPart(attachment); 

or 要么

URL url = new URL("http://greatproducts.com/gizmos/img.jpg");
DataHandler dataHandler = new DataHandler(url);
AttachmentPart attachment = message.createAttachmentPart(dataHandler);
attachment.setContentId("attached_image");

message.addAttachmentPart(attachment);

Accessing an AttachmentPart Object: 访问AttachmentPart对象:

java.util.Iterator iterator = message.getAttachments();
while (iterator.hasNext()) {
    AttachmentPart attachment = (AttachmentPart)iterator.next();
    String id = attachment.getContentId();
    String type = attachment.getContentType();
    System.out.print("Attachment " + id + " has content type " + type);
    if (type.equals("text/plain")) {
    Object content = attachment.getContent();
    System.out.println("Attachment contains:\n" + content);
    }
}

For more clarity on this process check this . 有关此过程的更多信息,请检查this

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM