如何设置JAXWS MTOM附件的文件名?

时间:2015-07-24 01:09:33

标签: java java-ee jax-ws mtom

我会在早上用代码示例更新这个,但是我有一个基本的JAX-WS实现,用于通过MTOM下载生成的文件,当我在SOAPUI中测试它时附加的文件有一个随机文件名甚至虽然我传递DataHandler的DataSource通过DataSource实现上的getName()方法具有正确的文件名。只想弄清楚我错过了什么。

这是我的终点

import javax.activation.DataHandler;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.xml.ws.soap.MTOM;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

@Controller
@WebService
@MTOM(enabled = true, threshold = 1024)
public class DataExtractsEndpoint extends SpringBeanAutowiringSupport{

    private final Logger LOGGER = Logger.getLogger(this.getClass());

    @Autowired
    private DataExtractDao dataExtractDao;

    @WebMethod
    public DataHandler getDownload() {
        LOGGER.debug(dataExtractDao.getDataSource().getName());
        LOGGER.debug(dataExtractDao.getDataSource().getContentType());
        DataHandler dh = new DataHandler(dataExtractDao.getDataSource());     
        return dh;
    }
}

我的自定义DataSource类

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.activation.DataSource;
import org.springframework.stereotype.Service;

@Service
public class ZipFileDataSource implements DataSource{

    private byte[] fileData;
    private final String fileContentType = "application/zip";
    private String fileName;
    private ByteArrayOutputStream baos;
    private ByteArrayInputStream bais;

    @Override
    public InputStream getInputStream() throws IOException {
        bais = new ByteArrayInputStream(fileData);
        return bais;
    }

    @Override
    public OutputStream getOutputStream() throws IOException {
        baos = new ByteArrayOutputStream();
        return baos;
    }

    public void saveOutputStream(){
        fileData = baos.toByteArray();
    }

    @Override
    public String getContentType() {
        return fileContentType;
    }

    @Override
    public String getName() {
        return fileName;
    }

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

}

示例文件名“attachment-2da67c29-c8de-455a-968c-a310eb470f8eexamplejaxwssuncom7224513233547289534.zip”

1 个答案:

答案 0 :(得分:1)

MTOM中未使用文件名。如果要发送文件名,请在XML中添加另一个元素。