Commit d9b03ee7 authored by 高东东's avatar 高东东

西飞文件传输webservice

parent af4d86b3
package com.yeejoin.amos.avic.face.model;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.activation.DataSource;
public class InputStreamDataSource implements DataSource{
private InputStream inputStream;
public InputStreamDataSource(InputStream inputStream) {
this.inputStream = inputStream;
}
@Override
public InputStream getInputStream() throws IOException {
return inputStream;
}
@Override
public OutputStream getOutputStream() throws IOException {
return parse(inputStream);
}
private ByteArrayOutputStream parse(final InputStream in) throws IOException {
final ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
int ch;
while ((ch = in.read()) != -1) {
swapStream.write(ch);
}
return swapStream;
}
@Override
public String getContentType() {
return "application/octet-stream";
}
@Override
public String getName() {
return "InputStreamDataSource";
}
}
package com.yeejoin.amos.avic.face.model;
import lombok.Data;
@Data
public class UploadFileModel {
String path;
String avicCode;
}
...@@ -2,7 +2,9 @@ package com.yeejoin.amos.avic.face.webservice; ...@@ -2,7 +2,9 @@ package com.yeejoin.amos.avic.face.webservice;
import javax.activation.DataHandler; import javax.activation.DataHandler;
import javax.jws.WebMethod; import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService; import javax.jws.WebService;
import javax.xml.bind.annotation.XmlMimeType;
import com.yeejoin.amos.avic.face.model.AvicCustomPathModel; import com.yeejoin.amos.avic.face.model.AvicCustomPathModel;
...@@ -17,7 +19,12 @@ public interface FileFransferService { ...@@ -17,7 +19,12 @@ public interface FileFransferService {
* @param path * @param path
*/ */
@WebMethod @WebMethod
public void useCodetransferFile(DataHandler handler, String fileName, String path, String code); public void useCodetransferFile(
@WebParam(name = "dataHandler")
@XmlMimeType(value = "application/octet-stream") DataHandler handler,
@WebParam(name = "fileName") String fileName,
@WebParam(name = "path") String path,
@WebParam(name = "code") String code);
/** /**
* 传输文件 * 传输文件
* @param handler * @param handler
...@@ -25,7 +32,11 @@ public interface FileFransferService { ...@@ -25,7 +32,11 @@ public interface FileFransferService {
* @param path * @param path
*/ */
@WebMethod @WebMethod
public void transferFile(DataHandler handler, String fileName, String path); public void transferFile(
@WebParam(name = "dataHandler")
@XmlMimeType(value = "application/octet-stream") DataHandler handler,
@WebParam(name = "fileName") String fileName,
@WebParam(name = "path") String path);
/** /**
* 传输目录配置 * 传输目录配置
......
...@@ -47,16 +47,17 @@ public class AvicCustomPathResource { ...@@ -47,16 +47,17 @@ public class AvicCustomPathResource {
@ApiOperation(value = "创建") @ApiOperation(value = "创建")
@RequestMapping(value = "", method = RequestMethod.POST) @RequestMapping(value = "", method = RequestMethod.POST)
public ResponseModel<AvicCustomPathModel> create(@RequestBody AvicCustomPathModel model) { public ResponseModel<AvicCustomPathModel> create(@RequestBody AvicCustomPathModel model) {
postConfig(model);
model = simpleService.createWithModel(model); model = simpleService.createWithModel(model);
return ResponseHelper.buildResponse(model); return ResponseHelper.buildResponse(model);
} }
public void postConfig() { public void postConfig(AvicCustomPathModel model) {
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient(webserviceUrl); Client client = dcf.createClient(webserviceUrl);
try { try {
// invoke("方法名",参数1,参数2,参数3....); // invoke("方法名",参数1,参数2,参数3....);
client.invoke("transferPathConfig", "张三"); client.invoke("transferPathConfig", model);
} catch (java.lang.Exception e) { } catch (java.lang.Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
...@@ -68,6 +69,7 @@ public class AvicCustomPathResource { ...@@ -68,6 +69,7 @@ public class AvicCustomPathResource {
public ResponseModel<AvicCustomPathModel> update(@RequestBody AvicCustomPathModel model, public ResponseModel<AvicCustomPathModel> update(@RequestBody AvicCustomPathModel model,
@PathVariable(value = "sequenceNbr") Long sequenceNbr) { @PathVariable(value = "sequenceNbr") Long sequenceNbr) {
model.setSequenceNbr(sequenceNbr); model.setSequenceNbr(sequenceNbr);
postConfig(model);
return ResponseHelper.buildResponse(simpleService.updateWithModel(model)); return ResponseHelper.buildResponse(simpleService.updateWithModel(model));
} }
......
package com.yeejoin.amos.avic.controller; package com.yeejoin.amos.avic.controller;
import java.io.File;
import java.io.InputStream;
import java.util.List;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import org.apache.cxf.endpoint.Client; import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory; import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.typroject.tyboot.core.foundation.enumeration.UserType; import org.typroject.tyboot.core.foundation.enumeration.UserType;
import org.typroject.tyboot.core.restful.doc.TycloudOperation; import org.typroject.tyboot.core.restful.doc.TycloudOperation;
import org.typroject.tyboot.core.restful.doc.TycloudResource; import org.typroject.tyboot.core.restful.doc.TycloudResource;
...@@ -16,6 +27,8 @@ import org.typroject.tyboot.core.restful.utils.ResponseHelper; ...@@ -16,6 +27,8 @@ import org.typroject.tyboot.core.restful.utils.ResponseHelper;
import org.typroject.tyboot.core.restful.utils.ResponseModel; import org.typroject.tyboot.core.restful.utils.ResponseModel;
import com.yeejoin.amos.avic.face.model.AvicCustomPathModel; import com.yeejoin.amos.avic.face.model.AvicCustomPathModel;
import com.yeejoin.amos.avic.face.model.InputStreamDataSource;
import com.yeejoin.amos.avic.face.model.UploadFileModel;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
...@@ -29,21 +42,35 @@ public class WebServicesFileFransferResource { ...@@ -29,21 +42,35 @@ public class WebServicesFileFransferResource {
@Value("${avic.webservice.path}") @Value("${avic.webservice.path}")
String webserviceUrl; String webserviceUrl;
@TycloudOperation(ApiLevel = UserType.AGENCY)
@TycloudOperation(ApiLevel = UserType.AGENCY, needAuth = false)
@ApiOperation(value = "传输文件") @ApiOperation(value = "传输文件")
@RequestMapping(value = "/file", method = RequestMethod.POST) @RequestMapping(value = "/file", method = RequestMethod.POST)
public ResponseModel postfile() { public ResponseModel postfile(@RequestPart MultipartFile[] files, @RequestParam String code, @RequestParam String path ) {
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient(webserviceUrl); Client client = dcf.createClient(webserviceUrl);
try { try {
// invoke("方法名",参数1,参数2,参数3....); if (ObjectUtils.isEmpty(code)) {
client.invoke("transferFile", "张三"); for (MultipartFile file : files) {
InputStream is = file.getInputStream();
DataHandler handler = new DataHandler(new InputStreamDataSource(is));
client.invoke("transferFile", handler, file.getOriginalFilename(), path);
}
} else {
for (MultipartFile file : files) {
InputStream is = file.getInputStream();
DataHandler handler = new DataHandler(new InputStreamDataSource(is));
client.invoke("useCodetransferFile", handler, file.getOriginalFilename(), path, code);
}
}
} catch (java.lang.Exception e) { } catch (java.lang.Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
return ResponseHelper.buildResponse(null); return ResponseHelper.buildResponse(null);
} }
@TycloudOperation(ApiLevel = UserType.AGENCY) @TycloudOperation(ApiLevel = UserType.AGENCY)
@ApiOperation(value = "传输目录配置") @ApiOperation(value = "传输目录配置")
@RequestMapping(value = "/config", method = RequestMethod.POST) @RequestMapping(value = "/config", method = RequestMethod.POST)
......
package com.yeejoin.amos.avic.service.impl; package com.yeejoin.amos.avic.service.impl;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import javax.activation.DataHandler; import javax.activation.DataHandler;
import javax.jws.WebService; import javax.jws.WebService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import com.yeejoin.amos.avic.face.model.AvicCustomPathModel; import com.yeejoin.amos.avic.face.model.AvicCustomPathModel;
import com.yeejoin.amos.avic.face.service.AvicCustomPathService;
import com.yeejoin.amos.avic.face.webservice.FileFransferService; import com.yeejoin.amos.avic.face.webservice.FileFransferService;
@WebService(serviceName = "FileFransferService", @WebService(serviceName = "FileFransferService",
...@@ -17,32 +22,69 @@ import com.yeejoin.amos.avic.face.webservice.FileFransferService; ...@@ -17,32 +22,69 @@ import com.yeejoin.amos.avic.face.webservice.FileFransferService;
@Component @Component
public class FileFransferServiceImpl implements FileFransferService { public class FileFransferServiceImpl implements FileFransferService {
@Autowired
AvicCustomPathService avicCustomPathService;
@Override @Override
public void useCodetransferFile(DataHandler handler, String fileName, String path, String code) { public void useCodetransferFile(DataHandler handler, String fileName, String path, String code) {
saveFile(handler, fileName, path);
sendEmail(code, fileName);
} }
@Override private void saveFile(DataHandler handler, String fileName, String path) {
public void transferFile(DataHandler handler, String fileName, String path) {
InputStream is = null; InputStream is = null;
FileOutputStream os = null;
try { try {
is = handler.getInputStream(); is = handler.getInputStream();
File file = new File(String.format("%s%s%s", path, File.separator, fileName));
File dir = new File(String.format("%s%s", path, File.separator));
if (!dir.exists()) {
dir.mkdirs();
}
if (!file.exists()) {
file.createNewFile();
}
os = new FileOutputStream(file);
int ch;
while ((ch = is.read()) != -1) {
os.write(ch);
}
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
}finally { }finally {
try { try {
if (is != null) {
is.close(); is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
if (os != null) {
try {
os.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
} }
}
private void sendEmail(String userCode, String fileName) {
}
@Override @Override
public void transferPathConfig(AvicCustomPathModel model) { public void transferFile(DataHandler handler, String fileName, String path) {
saveFile(handler, fileName, path);
}
@Override
public void transferPathConfig(AvicCustomPathModel model) {
if (ObjectUtils.isEmpty(model.getSequenceNbr())) {
avicCustomPathService.createWithModel(model);
} else {
avicCustomPathService.updateWithModel(model);
}
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment