天天看點

webservice檔案傳輸(服務端發送檔案給用戶端)

實體類

package com.axze.zzxc.entity;

//檔案類

import javax.activation.DataHandler;

import javax.xml.bind.annotation.XmlMimeType;

import javax.xml.bind.annotation.XmlType;

@XmlType(name = "CargoFile")

public class CargoFile {

    private String fileName;// 檔案名

    private long fileSize;// 檔案大小

    private DataHandler file;// 檔案二進制資料

    public String getFileName() {

        return fileName;

    }

    public void setFileName(String fileName) {

        this.fileName = fileName;

    }

    public long getFileSize() {

        return fileSize;

    }

    public void setFileSize(long fileSize) {

        this.fileSize = fileSize;

    }

    // 注解該字段為二進制流

    @XmlMimeType("application/octet-stream")

    public DataHandler getFile() {

        return file;

    }

    public void setFile(DataHandler file) {

        this.file = file;

    }

}

package com.axze.zzxc.entity;

//檔案資訊類

import java.util.Date;

public class CargoFileFromDB {

    private int fileid;// 檔案id

    private int opid;// 功能id

    private int pkid;// 入庫訂單id

    private String filename;// 檔案名

    private long filesize;// 檔案大小

    private Date credate;// 建立時間

    private String classname;// 檔案分類名稱

    private String steclassname;// 位置

    private String srcno;// 來源細單号

    private int updategoodsownerid;// 上傳貨主id

    public int getFileid() {

        return fileid;

    }

    public void setFileid(int fileid) {

        this.fileid = fileid;

    }

    public int getOpid() {

        return opid;

    }

    public void setOpid(int opid) {

        this.opid = opid;

    }

    public int getPkid() {

        return pkid;

    }

    public void setPkid(int pkid) {

        this.pkid = pkid;

    }

    public String getFilename() {

        return filename;

    }

    public void setFilename(String filename) {

        this.filename = filename;

    }

    public long getFilesize() {

        return filesize;

    }

    public void setFilesize(long filesize) {

        this.filesize = filesize;

    }

    public Date getCredate() {

        return credate;

    }

    public void setCredate(Date credate) {

        this.credate = credate;

    }

    public String getClassname() {

        return classname;

    }

    public void setClassname(String classname) {

        this.classname = classname;

    }

    public String getSteclassname() {

        return steclassname;

    }

    public void setSteclassname(String steclassname) {

        this.steclassname = steclassname;

    }

    public String getSrcno() {

        return srcno;

    }

    public void setSrcno(String srcno) {

        this.srcno = srcno;

    }

    public int getUpdategoodsownerid() {

        return updategoodsownerid;

    }

    public void setUpdategoodsownerid(int updategoodsownerid) {

        this.updategoodsownerid = updategoodsownerid;

    }

}

dao層接口

package com.axze.zzxc.dao;

import java.util.List;

import com.axze.zzxc.entity.CargoFileFromDB;

public interface CargoFileMapper {

    String getInId(String srcno, int goodsownerid);

    List<CargoFileFromDB> getCargoFileList(int opid, int pkid, String steclassname, String classname);

}

dao層xml

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

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.axze.zzxc.dao.CargoFileMapper">

    <resultMap id="BaseResultMap" type="com.axze.zzxc.entity.CargoFileFromDB">

        <id column="FILEID" jdbcType="DECIMAL" property="fileid" />

        <result column="OPID" jdbcType="DECIMAL" property="opid" />

        <result column="PKID" jdbcType="DECIMAL" property="pkid" />

        <result column="FILENAME" jdbcType="VARCHAR" property="filename" />

        <result column="INPUTMANID" jdbcType="DECIMAL" property="inputmanid" />

        <result column="CREDATE" jdbcType="TIMESTAMP" property="credate" />

        <result column="MODIFYMANID" jdbcType="DECIMAL" property="modifymanid" />

        <result column="MODIFYDATE" jdbcType="TIMESTAMP" property="modifydate" />

        <result column="FILESIZE" jdbcType="DECIMAL" property="filesize" />

        <result column="CLASSNAME" jdbcType="VARCHAR" property="classname" />

        <result column="STECLASSNAME" jdbcType="VARCHAR" property="steclassname" />

        <result column="SYS_MODIFYDATE" jdbcType="TIMESTAMP" property="sysModifydate" />

        <result column="SYNDATE" jdbcType="TIMESTAMP" property="syndate" />

        <result column="GOODSOWNERLICENSEID" jdbcType="DECIMAL" property="goodsownerlicenseid" />

        <result column="UPDATEGOODSOWNERID" jdbcType="DECIMAL" property="updategoodsownerid" />

    </resultMap>

    <select id="getInId" resultType="String">

        select t.inid from WMS_IN_ORDER t where t.srcno=#{arg0,jdbcType=VARCHAR} and t.goodsownerid=#{arg1}

    </select>

    <select id="getCargoFileList" resultType="com.axze.zzxc.entity.CargoFileFromDB">

        select * from np_efiles_op_file t 

        where t.opid = #{arg0} 

        and t.pkid = #{arg1}

        and t.steclassname = #{arg2,jdbcType=VARCHAR}

        and t.classname = #{arg3,jdbcType=VARCHAR}

    </select>

</mapper>

業務層接口

package com.axze.zzxc.service;

import java.util.List;

import javax.jws.WebMethod;

import javax.jws.WebService;

import com.axze.zzxc.entity.CargoFile;

@WebService(name = "CargoFileWS", targetNamespace = "http://www.tmp.com/services/file")

public interface SendCargoI {

    @WebMethod

    List<CargoFile> sendCargo(String json) throws Exception;

}

業務層實作類

package com.axze.zzxc.service;

import java.io.File;

import java.util.ArrayList;

import java.util.List;

import javax.activation.DataHandler;

import javax.activation.DataSource;

import javax.activation.FileDataSource;

import org.apache.ibatis.session.SqlSession;

import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSON;

import com.axze.zzxc.dao.CargoFileMapper;

import com.axze.zzxc.dao.LicenseSecretkeyMapper;

import com.axze.zzxc.entity.CargoFile;

import com.axze.zzxc.entity.CargoFileFromDB;

import com.axze.zzxc.entity.InvoiceFileInfoFromHZ;

import com.axze.zzxc.entity.LicenseSecretkey;

import com.axze.zzxc.tools.MyBatisUtil;

public class SendCargoImpl implements SendCargoI {

    @Override

    public List<CargoFile> sendCargo(String json) throws Exception {

        Logger log = Logger.getLogger(SendCargoImpl.class);

        long start = System.currentTimeMillis();

        InvoiceFileInfoFromHZ ifg = JSON.parseObject(json, InvoiceFileInfoFromHZ.class);

        SqlSession ss = MyBatisUtil.getSqlSession();

        CargoFileMapper cfm = ss.getMapper(CargoFileMapper.class);

        LicenseSecretkeyMapper lskm = ss.getMapper(LicenseSecretkeyMapper.class);

        List<LicenseSecretkey> selectByPrimaryKey = lskm.selectByPrimaryKey(ifg.getGoodsownerid(), ifg.getSecretkey(), 3);

        if (!(null != selectByPrimaryKey && 1 == selectByPrimaryKey.size())) {

            throw new Exception("貨主id或秘鑰錯誤,傳輸失敗!");

        }

        String inId = cfm.getInId(ifg.getSrcno(), ifg.getGoodsownerid());

        if(null == inId) {

            throw new Exception("沒有查到該入庫訂單,請聯系業務人員核實!");

        }

        int opid = 4401;

        int pkid = Integer.parseInt(inId);

        String steclassname = "物流入庫訂單總單";

        String classname = "随貨同行";

        CargoFile cargoFile = null;

        ArrayList<String> filePaths = new ArrayList<>();

        ArrayList<CargoFile> cargoFileList = new ArrayList<>();

        List<CargoFileFromDB> cargoFileList2 = cfm.getCargoFileList(opid, pkid, steclassname, classname);

        for (CargoFileFromDB cargoFileFromDB : cargoFileList2) {

            filePaths.add(getUrl(opid, pkid, steclassname, classname) + cargoFileFromDB.getFilename());

        }

        for (String string : filePaths) {

            cargoFile = constructFileEntity(string);

            cargoFileList.add(cargoFile);

        }

        long end = System.currentTimeMillis();

        log.info("parameter:" + json + "\n" + "time:" + (end - start) + "hs");

        return cargoFileList;

    }

    public String getUrl(int opid, int pkid, String steclassname, String classname) {

        return "C:\\電子檔案根目錄\\功能電子檔案\\" + opid + "\\" + steclassname + "\\" + pkid + "\\" + classname + "\\";

    }

    private static CargoFile constructFileEntity(String filePath) {

        CargoFile fileEntity = new CargoFile();

        File file = new File(filePath);

        fileEntity.setFileName(file.getName());

        fileEntity.setFileSize(file.length());

        DataSource source = new FileDataSource(file);

        DataHandler handler = new DataHandler(source);

        fileEntity.setFile(handler);

        return fileEntity;

    }

}

用戶端實作類

package cc;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.List;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SendFile {

    public static void main(String[] args) {

        uploadFile(getFile("{\"secretkey\":\"zhengzhaochuanshu_xizangchengyi#10321\",\"goodsownerid\":\"21\",\"srcno\":\"2017110201\"}"));

    }

    public static List<CargoFile> getFile(String json){

        List<CargoFile> cargoFiles = null;

        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

        CargoFileWS fileWS = applicationContext.getBean("sendFile",CargoFileWS.class);

        cargoFiles = fileWS.sendCargo(json);

        return cargoFiles;

    }

    public static String uploadFile(List<CargoFile> file) {

        String result = null;

        OutputStream os = null;

        InputStream is = null;

        BufferedOutputStream bos = null;

        try {

            for (CargoFile invoiceFile : file) {

                is = invoiceFile.getFile().getInputStream();

                String path = "D:\\電子檔案根目錄\\功能電子檔案\\";

                File dest1 = new File(path);

                if (!dest1.exists()) {

                    dest1.mkdirs();

                }

                File dest = new File(path + invoiceFile.getFileName());

                os = new FileOutputStream(dest);

                bos = new BufferedOutputStream(os);

                byte[] buffer = new byte[1024];

                int len = 0;

                while ((len = is.read(buffer)) != -1) {

                    bos.write(buffer, 0, len);

                }

                bos.flush();

            }

            result = "傳輸成功";

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            if (bos != null) {

                try {

                    bos.close();

                } catch (Exception e) {

                }

            }

            if (os != null) {

                try {

                    os.close();

                } catch (Exception e) {

                }

            }

            if (is != null) {

                try {

                    is.close();

                } catch (Exception e) {

                }

            }

        }

        return result;

    }

}

轉載于:https://my.oschina.net/u/3639290/blog/1592922