实体类
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 {
/**
* 文件上传
* @param file 文件上传包装类
* @return 上传成功返回true,上传失败返回false。
* @throws Exception
*/
@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;
}
/**
* 获取文件路径
*
* @param opid
* @param pkid
* @param steclassname
* @param classname
* @return
*/
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;
}
/**
* 上传文件
*
* @param file
* 文件集合
* @param opid
* 功能id
* @param pkid
* 细单id
* @param classname
* 文件分类名称
* @param steclassname
* 位置
* @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:\\电子档案根目录\\功能电子档案\\";
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;
}
}