java datahandler_用DataHandler来实现一个带附件的soap请求的web services

本文介绍了如何使用Java的DataHandler类来创建一个支持文本和二进制文件的Web服务。通过部署描述文件deploy.wsdd配置,实现了将文件作为SOAP请求的一部分进行上传和下载。在服务端,数据被读取并保存到指定目录。客户端通过调用服务并传递DataHandler对象来执行操作。成功执行后,上传的文件会被保存在特定的文件夹中。
摘要由CSDN通过智能技术生成

1.为了使其对DataHandler的支持。除了配置好axis环境之外,还要在sun的网站上下载jaf 1-0-2.jar包,并把它注册到CLASS_PATH中。

2.编写服务程序(.java),既支持文本文件,也支持二进制文件。

package test.gaolong;

import java.io.*;

import javax.activation.*;

public class FileService{

public static String Repository="./files/";

public String putFile(DataHandler dh,String name){

if(name==null)

name="test.tmp";

System.out.println("test");

try{

File dir=new File(Repository);

if(!dir.exists()){

dir.mkdir(); System.out.println("makedir"+"test");

}

InputStream input=dh.getInputStream();

FileOutputStream fos=new FileOutputStream(new File(dir,name));

System.out.println("test");

byte[] buffer=new byte[1024*4];

int n=0;

while((n=input.read(buffer))!=-1){

fos.write(buffer,0,n);

System.out.println(buffer);

}

System.out.println("test");

input.close();

fos.close();

}catch(IOException e){

e.printStackTrace();

}

return name+"send OK";

}

public DataHandler getFile(String name){

File dir=new File(Repository);

if(!dir.exists())

dir.mkdir();

File data=new File(dir,name);

if(data.exists())

return new DataHandler(new FileDataSource(data));

else

return null;

}

}

3。写deploy.wsdd部署描述文件如下:

DataHandler dh=new DataHandler(new FileDataSource(filename));

String endpoint="http://127.0.0.1:7003/axis/services/FileService";

String name="gaolong1";

Service service=new Service();

Call call=(Call) service.createCall();

call.setTargetEndpointAddress(new java.net.URL(endpoint));

call.setOperationName(new QName("http://127.0.0.1:7001/axis/services/FileService", "putFile"));//指定方法的命名空间

QName qnameattachment=new QName("FileService","DataHandler");

call.registerTypeMapping(dh.getClass(),qnameattachment,JAFDataHandlerSerializerFactory.class,JAFDataHandlerDeserializerFactory.class);

call.addParameter("s1",qnameattachment,ParameterMode.IN);

call.addParameter("s2",XMLType.XSD_STRING,ParameterMode.IN);

call.setReturnType(XMLType.XSD_STRING);//XMLType.XSD_STRING);//用Class.forName("java.lang.String")来获取java类型

String ret=(String)call.invoke(new Object[] {dh,"HelloWorld.java"});

System.out.println(ret);

}

}

6。成功执行客户端应用程序,可以找到上传文件。

请求的soap消息:

POST /axis/services/FileService HTTP/1.0

Content-Type: multipart/related; type="text/xml"; start="<3165C8664597DC7EF29D5BFAC8972562>";  boundary="----=_Part_0_21866740.1141202759484"

Accept: application/soap+xml, application/dime, multipart/related, text/*

User-Agent: Axis/1.2.1

Host: localhost:7003

Cache-Control: no-cache

Pragma: no-cache

SOAPAction: ""

Content-Length: 1050

------=_Part_0_21866740.1141202759484

Content-Type: text/xml; charset=UTF-8

Content-Transfer-Encoding: binary

Content-Id: <3165C8664597DC7EF29D5BFAC8972562>

<?xml version="1.0" encoding="UTF-8"?>HelloWorld.java

------=_Part_0_21866740.1141202759484

Content-Type: application/octet-stream

Content-Transfer-Encoding: binary

Content-Id: <03D9C1D3A9E0788D274934C3ABD52811>

public class HelloWorld{

public String sayHello(String name){

return "Hello"+name;

}

}

------=_Part_0_21866740.1141202759484--

上传后的文件的位置:/*在该目录下创建文件夹:D:\bea\user_projects\domains\mydomain\files,并把相应的文件存入该目录下*/

7.另一种基于java mail的带附件的传输,是基于xmlDOM+servlet可以来实现,只是比较底层而已。

好的,以下是Java代码实现发送代码生成的附件的邮件: ```java import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.*; import javax.mail.internet.*; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.nio.charset.StandardCharsets; public class SendEmail { public static void main(String[] args) throws MessagingException, IOException { // 发件人邮箱地址 String from = "your_email@example.com"; // 发件人邮箱密码或授权码 String password = "your_email_password_or_authorization_code"; // 收件人邮箱地址 String to = "recipient_email@example.com"; // 邮件主题 String subject = "邮件主题"; // 代码文件路径 String codeFilePath = "code_file_path"; // 代码文件名 String codeFileName = "code_file_name"; // 代码文件类型 String codeFileType = "code_file_type"; // 创建邮件会话 Properties props = new Properties(); props.setProperty("mail.transport.protocol", "smtp"); props.setProperty("mail.smtp.host", "smtp.example.com"); props.setProperty("mail.smtp.auth", "true"); Session session = Session.getDefaultInstance(props, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(from, password); } }); // 创建邮件消息 MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.setRecipient(Message.RecipientType.TO, new InternetAddress(to)); message.setSubject(subject); // 创建代码文件 File codeFile = new File(codeFilePath, codeFileName + "." + codeFileType); FileWriter fileWriter = new FileWriter(codeFile); fileWriter.write("public class HelloWorld {\n" + " public static void main(String[] args) {\n" + " System.out.println(\"Hello, world!\");\n" + " }\n" + "}\n"); fileWriter.close(); // 创建附件 MimeBodyPart attachmentPart = new MimeBodyPart(); DataSource source = new FileDataSource(codeFile); attachmentPart.setDataHandler(new DataHandler(source)); attachmentPart.setFileName(MimeUtility.encodeText(codeFileName + "." + codeFileType, StandardCharsets.UTF_8.name(), null)); // 创建邮件内容 MimeMultipart multipart = new MimeMultipart(); multipart.addBodyPart(attachmentPart); message.setContent(multipart); // 发送邮件 Transport.send(message); } } ``` 其中,需要替换的参数有: - `from`: 发件人邮箱地址 - `password`: 发件人邮箱密码或授权码 - `to`: 收件人邮箱地址 - `subject`: 邮件主题 - `codeFilePath`: 代码文件路径 - `codeFileName`: 代码文件名 - `codeFileType`: 代码文件类型 以上代码中,我们先创建了一个代码文件,并将其保存到指定路径下。然后,我们创建一个`MimeBodyPart`对象,将代码文件作为附件添加到邮件中。最后,我们使用`Transport.send()`方法发送邮件。 需要注意的是,我们在设置附件的文件名时,使用了`MimeUtility.encodeText()`方法进行编码,以解决中文文件名乱码的问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值