webservice file transfer (the server sends the file to the client)

entity class

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;// file name
    private long fileSize;// file size
    private DataHandler file;// file binary data
    
    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;
    }
    // Annotate this field as binary stream
    @XmlMimeType("application/octet-stream")
    public DataHandler getFile() {
        return file;
    }
    public void setFile(DataHandler file) {
        this.file = file;
    }
    
}
 

 

package com.axze.zzxc.entity;
//File information class
import java.util.Date;

public class CargoFileFromDB {
    
    private int fileid;// file id
    private int opid;// function id
    private int pkid;// warehousing order id
    private String filename;// file name
    private long filesize;// file size
    private Date credate; // Creation time
    private String classname;// File classification name
    private String steclassname;// Location
    private String srcno;// Source details
    private int updategoodsownerid;// Upload owner 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 layer interface

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 layers 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>

 

business layer interface

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 {

    /**
     * File upload
     * @param file File upload wrapper class
     * @return Returns true for upload success, false for upload failure.
     * @throws Exception 
     */
    @WebMethod
    List<CargoFile> sendCargo(String json) throws Exception;
}
 

Business layer implementation class

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("The owner id or secret key is wrong, transmission failed!");
        }
        String inId = cfm.getInId(ifg.getSrcno(), ifg.getGoodsownerid());
        if(null == inId) {
            throw new Exception ("The inbound order is not found, please contact the sales staff to verify!");
        }
        int opid = 4401;
        int pkid = Integer.parseInt(inId);
        String steclassname = "logistics inbound order total";
        String classname = "Go with the cargo";
        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;
    }

    /**
     * Get file path
     * 
     * @param opid
     * @param pkid
     * @param steclassname
     * @param classname
     * @return
     */
    public String getUrl(int opid, int pkid, String steclassname, String classname) {
        return "C: \\electronic file root directory\\function electronic file\\" + 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;
    }
    
}
 

Client implementation class

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;      * @param opid      * file collection      * @param file      *       * upload file     /**
    }
    






     * function id
     * @param pkid
     * detail list id
     * @param classname
     * file classification name
     * @param steclassname
     * location
     * @return
     */
    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:\\electronic file root\\function electron File\\";
                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;
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325499140&siteId=291194637