ssm调用webservice接口并进行文件传输
2021-01-17 17:13
标签:创建 comm address lse ping 工具 java info iterator ssm调用接口时大致分为三步, 1,Java后台使用 MultipartFile 接收文件后将类型转为 file , 2,将file进行base64加密 3,调用接口进行传输 ssm调用webservice代码: 文件转换工具类 插入数据拼接xml格式报文 webservice服务端 ssm调用webservice接口并进行文件传输 标签:创建 comm address lse ping 工具 java info iterator 原文地址:https://www.cnblogs.com/ljmm/p/13362029.htmlimport org.apache.commons.fileupload.disk.DiskFileItem;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import com.alibaba.fastjson.JSONObject;
/**
* 插入数据测试接口
* @param request
* @param fileONE
* @param fileTOW
* @return
*/
@RequestMapping("/webJKInsert")
@ResponseBody
public Mappackage com.ljs.utils;
import java.io.*;
import java.util.Base64;
/**
* 文件转换工具类
*/
public class MulFileUtil {
/**
* file转换为base64
* @param file
* @return
* @throws IOException
*/
public static String fileBase64(File file) throws IOException {
String base64 = null;
InputStream in = null;
try {
in = new FileInputStream(file);
byte[] bytes=new byte[(int)file.length()];
in.read(bytes);
base64 = Base64.getEncoder().encodeToString(bytes);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return base64;
}
/**
* base64转file并写入磁盘
* @param str
* @param fileName
* @return
* @throws UnsupportedEncodingException
*/
public static File base64File(String str,String fileName) throws UnsupportedEncodingException {
File file = null;
//创建文件目录
String filePath="/webJKFile";
File dir=new File(filePath);
if (!dir.exists() && !dir.isDirectory()) {
dir.mkdirs();
}
BufferedOutputStream bos = null;
java.io.FileOutputStream fos = null;
try {
byte[] bytes = Base64.getDecoder().decode(str);
file=new File(filePath+"\\"+fileName);
fos = new java.io.FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(bytes);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return file;
}
}
package com.ljs.utils;
/**
* 插入数据xml
*/
public class InsertXML {
public static String insertNotice(String assessment,String research){
String xml="";
StringBuilder builder = new StringBuilder();
builder.append("");
builder.append("
/**
* 数据新增
* @param xml
* @return
*/
public String getInsert(String xml){
System.out.println("客户端正在调用新增方法......");
Map
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/jk?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true", "root", "root");
PreparedStatement preparedStatement;
doc = DocumentHelper.parseText(xml); // 将字符串转为XML
Element rootElt = doc.getRootElement(); // 获取根节
Iterator iterss = rootElt.elementIterator("Body"); ///获取根节点下的子节点body
// 遍历body节点
while (iterss.hasNext()) {
Element recordEless = (Element) iterss.next();
String sysPass = recordEless.elementTextTrim("sysPass"); // 拿到body节点下的子节点sysPass值
Iterator itersElIterator = recordEless.elementIterator("prop"); // 获取子节点body下的子节点prop
// 遍历prop节点下的Response节点
while (itersElIterator.hasNext()) {
Element itemEle = (Element) itersElIterator.next();
//map.put("projectName",itemEle.elementTextTrim("projectName")); // 拿到body下的子节点prop下的字节点projectName的值
SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//获取系统时间
//报告1上传
if(itemEle.elementTextTrim("assessment")!=null && !"".equals(itemEle.elementTextTrim("assessment")) && !"null".equals(itemEle.elementTextTrim("assessment"))){
System.out.println("正在上传报告1···"+itemEle.elementTextTrim("assessment"));
String uuid = UUID.randomUUID().toString();//生成文件唯一编号
String[] split = itemEle.elementTextTrim("assessment").split(",");//分割获取文件名和经过base64转码后的字符串
String assessmentName = split[0];//文件名
String assessmentFile = split[1];//转码后的字符串
Map
文章标题:ssm调用webservice接口并进行文件传输
文章链接:http://soscw.com/index.php/essay/43280.html