做了一个测试案例,用的沙箱测试账号
可以去支付宝官网注册一个开发者账号,获取沙箱账号,地址: https://openhome.alipay.com/platform/appDaily.htm?tab=info
注册完之后会看到以下画面:
上面两个图中,我们可以看到有这个几个有用的信息:
1.APPID
2.支付宝网关
3.商家账号 登录密码
4.买家账号 登录密码 支付密码
接下来,我们在沙箱应用里要设置一下RSA2公钥
首先下载生成公钥和私钥的工具,地址:https://opendocs.alipay.com/open/291/105971#LDsXr
如上图,只有windows版和macOs版,用linux的心疼一秒钟,还好我有windows虚拟机。
如上图,安装之后,点击生成秘钥即可生成公钥和私钥,复制保存好这个公钥和私钥
接下来更改沙箱应用中RSA2的公钥,把刚刚生成的公钥复制到在线网站上保存。
到现在为止,准备工作已经做完了。
在springboot上创建一个AlipayConfig类
package com.zhaohy.app.config;
import java.io.FileWriter;
import java.io.IOException;
public class AlipayConfig {
// 应用ID,您的APPID,收款账号既是您的APPID对应支付宝账号
public static String APP_ID = "";
// 商户私钥,您的PKCS8格式RSA2私钥,这些就是我们刚才设置的
public static String RSA_PRIVATE_KEY = "";
// 支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm
// 对应APPID下的支付宝公钥。,这些就是我们刚才设置的
public static String ALIPAY_PUBLIC_KEY = "";
// 异步通知,再这里我们设计自己的后台代码
public static String notify_url = "http://www.zhaohy.org.cn";
// 页面跳转同步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
public static String return_url = "http://www.zhaohy.org.cn";
// 签名方式
public static String SIGN_TYPE = "RSA2";
// 字符编码格式
public static String CHARSET = "utf-8";
// 支付宝网关 https://openapi.alipaydev.com/gateway.do https://openapi.alipay.com/gateway.do
public static String GATEWAYURL = "https://openapi.alipaydev.com/gateway.do";
// 支付宝网关
public static String LOG_PATH = "/home/zhaohy/alipayLog/";
public static String FORMAT = "json";
// ↑↑↑↑↑↑↑↑↑↑请在这里配置您的基本信息↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
/**
* 写日志,方便测试(看网站需求,也可以改成把记录存入数据库)
*
* @param sWord 要写入日志里的文本内容
*/
public static void logResult(String sWord) {
FileWriter writer = null;
try {
writer = new FileWriter(LOG_PATH + "alipay_log_" + System.currentTimeMillis() + ".txt");
writer.write(sWord);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
把之前的appid,公钥,私钥和网关地址都填在上面代码里
新建AlipayService类
package com.zhaohy.app.service;
import com.alipay.api.AlipayApiException;
public interface AlipayService {
/**
* web端订单支付
*
* @param outTradeNo 订单编号(唯一)
* @param totalAmount 订单价格
* @param subject 商品名称
*/
String webPagePay(String outTradeNo, Integer totalAmount, String subject) throws Exception;
/**
* app端订单支付
*
* @param outTradeNo 订单编号
* @param totalAmount 订单价格
* @param subject 商品名称
*/
String appPagePay(String outTradeNo, Integer totalAmount, String subject) throws Exception;
/**
* 退款
*
* @param outTradeNo 订单编号
* @param refundReason 退款原因
* @param refundAmount 退款金额
* @param outRequestNo 标识一次退款请求,同一笔交易多次退款需要保证唯一,如需部分退款,则此参数必传
*/
String refund(String outTradeNo, String refundReason, Integer refundAmount, String outRequestNo)
throws AlipayApiException;
/**
* 交易查询
*
* @param outTradeNo 订单编号(唯一)
*/
String query(String outTradeNo) throws AlipayApiException;
/**
* 交易关闭
*
* @param outTradeNo订单编号(唯一)
*/
String close(String outTradeNo) throws AlipayApiException;
/**
* 退款查询
*
* @param outTradeNo 订单编号(唯一)
* @param outRequestNo 标识一次退款请求,同一笔交易多次退款需要保证唯一,如需部分退款,则此参数必传
*/
String refundQuery(String outTradeNo, String outRequestNo) throws AlipayApiException;
}
新建AlipayServiceImpl实现类
package com.zhaohy.app.service.impl;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Service;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeCloseRequest;
import com.alipay.api.request.AlipayTradeFastpayRefundQueryRequest;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.zhaohy.app.config.AlipayConfig;
import com.zhaohy.app.service.AlipayService;
import com.zhaohy.app.utils.AppFrameworkUtil;
@Service("AlipayService")
public class AlipayServiceImpl implements AlipayService {
/** 调取支付宝接口 web端支付 */
DefaultAlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.GATEWAYURL, AlipayConfig.APP_ID,
AlipayConfig.RSA_PRIVATE_KEY, "json", AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY,
AlipayConfig.SIGN_TYPE);
/** 调取支付宝接口app端支付 */
AlipayClient alipayClients = new DefaultAlipayClient(AlipayConfig.GATEWAYURL, AlipayConfig.APP_ID,
AlipayConfig.RSA_PRIVATE_KEY, "json", AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY,
AlipayConfig.SIGN_TYPE);
/**
* web端订单支付
*
* @param outTradeNo 订单编号(唯一)
* @param totalAmount 订单价格
* @param subject 商品名称
*/
@Override
public String webPagePay(String outTradeNo, Integer totalAmount, String subject) throws Exception {
AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
/** 同步通知,支付完成后,支付成功页面 */
alipayRequest.setReturnUrl(AlipayConfig.return_url);
/** 异步通知,支付完成后,需要进行的异步处理 */
alipayRequest.setNotifyUrl(AlipayConfig.notify_url);
Map bizContentMap = new HashMap();
bizContentMap.put("out_trade_no", outTradeNo);
bizContentMap.put("total_amount", totalAmount + "");
bizContentMap.put("subject", subject);
bizContentMap.put("body", "商品名称");
bizContentMap.put("timeout_express", "90m");
bizContentMap.put("product_code", "FAST_INSTANT_TRADE_PAY");
String bizContentJson = AppFrameworkUtil.structureConfigParamsGroupJSONDataWithOutSimpleResultBean(bizContentMap);
alipayRequest.setBizContent(bizContentJson);
/** 转换格式 */
String result = alipayClient.pageExecute(alipayRequest).getBody().replace('\"', '\'').replace('\n', ' ');
return result;
}
/**
* 退款
*
* @param outTradeNo 订单编号
* @param refundReason 退款原因
* @param refundAmount 退款金额
* @param outRequestNo 标识一次退款请求,同一笔交易多次退款需要保证唯一,如需部分退款,则此参数必传
*/
@Override
public String refund(String outTradeNo, String refundReason, Integer refundAmount, String outRequestNo)
throws AlipayApiException {
AlipayTradeRefundRequest alipayRequest = new AlipayTradeRefundRequest();
/** 调取接口 */
alipayRequest.setBizContent("{\"out_trade_no\":\"" + outTradeNo + "\"," + "\"refund_amount\":\"" + refundAmount
+ "\"," + "\"refund_reason\":\"" + refundReason + "\"," + "\"out_request_no\":\"" + outRequestNo
+ "\"}");
String result = alipayClient.execute(alipayRequest).getBody();
return result;
}
/**
* 交易查询
*
* @param outTradeNo 订单编号(唯一)
*/
@Override
public String query(String outTradeNo) throws AlipayApiException {
AlipayTradeQueryRequest alipayRequest = new AlipayTradeQueryRequest();
/** 请求接口 */
alipayRequest.setBizContent("{\"out_trade_no\":\"" + outTradeNo + "\"," + "\"trade_no\":\"" + "" + "\"}");
/** 转换格式 */
String result = alipayClient.execute(alipayRequest).getBody();
return result;
}
/**
* 交易关闭
*
* @param outTradeNo订单编号(唯一)
*/
@Override
public String close(String outTradeNo) throws AlipayApiException {
AlipayTradeCloseRequest alipayRequest = new AlipayTradeCloseRequest();
alipayRequest.setBizContent("{\"out_trade_no\":\"" + outTradeNo + "\"," + "\"trade_no\":\"" + "" + "\"}");
String result = alipayClient.execute(alipayRequest).getBody();
return result;
}
/**
* 退款查询
*
* @param outTradeNo 订单编号(唯一)
* @param outRequestNo 标识一次退款请求,同一笔交易多次退款需要保证唯一,如需部分退款,则此参数必传
*/
@Override
public String refundQuery(String outTradeNo, String outRequestNo) throws AlipayApiException {
AlipayTradeFastpayRefundQueryRequest alipayRequest = new AlipayTradeFastpayRefundQueryRequest();
/** 请求接口 */
alipayRequest.setBizContent(
"{\"out_trade_no\":\"" + outTradeNo + "\"," + "\"out_request_no\":\"" + outRequestNo + "\"}");
/** 格式转换 */
String result = alipayClient.execute(alipayRequest).getBody();
return result;
}
/**
* app端订单支付
*
* @param outTradeNo 订单编号
* @param totalAmount 订单价格
* @param subject 商品名称
*/
@Override
public String appPagePay(String outTradeNo, Integer totalAmount, String subject) throws Exception {
AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();
/** 同步通知,支付完成后,支付成功页面 */
alipayRequest.setReturnUrl(AlipayConfig.return_url);
/** 异步通知,支付完成后,需要进行的异步处理 */
alipayRequest.setNotifyUrl(AlipayConfig.notify_url);
/** 销售产品码(固定) */
String productCode = "QUICK_WAP_WAY";
/** 进行赋值 */
AlipayTradeWapPayModel wapPayModel = new AlipayTradeWapPayModel();
wapPayModel.setOutTradeNo(outTradeNo);
wapPayModel.setSubject(subject);
wapPayModel.setTotalAmount(totalAmount.toString());
wapPayModel.setBody("商品名称");
wapPayModel.setTimeoutExpress("2m");
wapPayModel.setProductCode(productCode);
alipayRequest.setBizModel(wapPayModel);
/** 格式转换 */
String result = alipayClients.pageExecute(alipayRequest).getBody().replace('\"', '\'').replace('\n', ' ');
return result;
}
}
新建AlipayController
package com.zhaohy.app.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.zhaohy.app.service.AlipayService;
import com.zhaohy.app.utils.AppFrameworkUtil;
@Controller
public class AlipayController {
@Autowired
private AlipayService alipayService;
@RequestMapping("/alipayTest.do")
public String test(HttpServletRequest request) {
request.setAttribute("ctx", AppFrameworkUtil.getWebRootUrl(request, ""));
return "html/alipay";
}
/**
* web 订单支付
*/
@RequestMapping("/alipay/getPagePay")
public void getPagePay(HttpServletRequest request, HttpServletResponse response) throws Exception {
/** 模仿数据库,从后台调数据 */
String outTradeNo = "19960310621211";
Integer totalAmount = 1;
String subject = "苹果28";
String pay = alipayService.webPagePay(outTradeNo, totalAmount, subject);
System.out.println(pay);
Map
controller里getAppPagePay是app订单支付,里面写死的是测试数据,如果正式用,可以当参数传进来。
演示app 订单支付,因为app订单支付会跳转到支付宝进行登录支付,如果没有监测到支付宝应用,则用网页版的支付宝进行支付,会比较方便。
调用上面的/alipayTest.do接口会跳转到html/alipay.html页面
alipay.html代码如下:
Insert title here
可以看到,一进来这个页面会请求app订单支付接口,那个接口会返回一个form表单,并提交给支付宝进行支付,那个form表单的html放到#alipay div中便会自动执行跳转.
启动项目,访问http://127.0.0.1:8082/alipayTest.do
如图,电脑访问跳转到了上图页面,点击继续浏览器付款,登录沙箱买家账号,输入支付密码即可
点击确认付款,输入密码
几秒钟后会跳转回AlipayConfig里面设置的http:www.zhaohy.org.cn
至此,整个支付过程已经完成~
=====2021年7月19日更新=====
补充一下AppFrameworkUtil工具类
package com.zhaohy.app.utils;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.imageio.ImageIO;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.ibatis.session.SqlSession;
import org.springframework.context.ApplicationContext;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zhaohy.app.service.UserService;
import flexjson.JSONSerializer;
import nl.bitwalker.useragentutils.UserAgent;
public class AppFrameworkUtil {
/**
* 四舍五入。保留num个小数点后的数字
*
* @param bh
* @param num
* @return
*/
public static String getNum(double bh, int num) {
String numstr = "";
numstr = new BigDecimal(bh).setScale(num, BigDecimal.ROUND_HALF_UP) + "";
return numstr;
}
/**
* 去替换横杠
*
* @param str
* @param t_str
* @return
*/
public static String getReplaseStr(String str) {
String t_str = null;
if (str != null) {
t_str = str.replace("-", "");
}
return t_str;
}
/**
* 四舍五入。保留num个小数点后的数字
*
* @param bh
* @param num
* @return
*/
public static String getNum(String bh, int num) {
if (AppFrameworkUtil.isEmpty(bh) || "-".equals(bh))
return "-";
if ("0.0".equals(bh) || "0".equals(bh))
return bh;
String numstr = "";
numstr = new BigDecimal(Double.parseDouble(bh)).setScale(num, BigDecimal.ROUND_HALF_UP) + "";
return numstr;
}
/**
* 字符串空判断
*
* @param str
* @return
*/
public static boolean isEmpty(String str) {
boolean flag = true;
if (null != str && 0 != str.trim().length())
flag = false;
return flag;
}
/**
* 添加符号
*
* @param str
*/
public static String addSign(String str) {
if (!AppFrameworkUtil.isEmpty(str) && !str.contains("-")) {
str = "+" + str;
}
return str;
}
/**
* 添加千分位
*
* @param str
* @return
*/
public static String format(String str) {
if (!isEmpty(str) && !"-".equals(str)) {
DecimalFormat df = new DecimalFormat("###,###");
try {
str = df.format(Long.parseLong(str));
} catch (Exception e) {
return str;
}
}
return str;
}
/**
* response JSON数据
*
* @param response
* @param context
*/
public static void renderJSON(HttpServletResponse response, String context) {
PrintWriter out = null;
try {
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json");
out = response.getWriter();
out.write(context);
out.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
out.close();
}
}
/**
* 获取外网IP
*
* @param request
* @return
*/
public static String getRemoteHost(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ip;
}
public static String getSystemDate() {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String curDate = s.format(calendar.getTime()); // 当前日期
return curDate;
}
/**
*
* @param str
* @return
*/
public static boolean isBlank(String str) {
return (str == null || str.trim().length() == 0 || str.equals("undefined") || str.equals("null"));
}
/**
* 序列化集合成JSON字符
* @param list
* @return
*/
@SuppressWarnings("unchecked")
public static String structureConfigParamsGroupJSONData(Object obj) {
JSONSerializer serializer = new JSONSerializer();
String json = "";
@SuppressWarnings("rawtypes")
SimpleResultBean result = new SimpleResultBean();
result.setData(obj);
json = serializer.exclude("*.class").deepSerialize(result).replaceAll(":\\s*null\\s*", ":\"\"");
return json;
}
public static String structureConfigParamsGroupJSONDataWithOutSimpleResultBean(Object obj) {
JSONSerializer serializer = new JSONSerializer();
String json = "";
json = serializer.exclude("*.class").deepSerialize(obj).replaceAll(":\\s*null\\s*", ":\"\"");
return json;
}
/**
* 返回jsonObj参数
*
* @param request
* @return
*/
public static JSONObject getRequestParamsObj(HttpServletRequest request) {
JSONObject paramsObj = new JSONObject();
try {
BufferedReader streamReader = new BufferedReader(new InputStreamReader(request.getInputStream(), "UTF-8"));
StringBuilder responseStrBuilder = new StringBuilder();
String inputStr;
while ((inputStr = streamReader.readLine()) != null)
responseStrBuilder.append(inputStr);
paramsObj = JSONObject.parseObject(responseStrBuilder.toString());
} catch (Exception e) {
e.printStackTrace();
}
return paramsObj;
}
/**
* 判断是否是ajax请求
*
* @param request
* @return
*/
public static boolean isAjax(HttpServletRequest request) {
String accept = request.getHeader("accept");
if (accept != null && accept.indexOf("application/json") != -1) {
return true;
} else {
return false;
}
}
/**
*
* 获取用户mac地址
*
* @param ip
* @return
*/
public static String getMacAddrByIp(String ip) {
String macAddr = null;
try {
String command = "nbtstat -a ";
if (!System.getProperty("os.name").contains("Windows")) {
command = "netstat -a ";
}
Process process = Runtime.getRuntime().exec(command + ip);
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
Pattern pattern = Pattern.compile("([A-F0-9]{2}-){5}[A-F0-9]{2}");
Matcher matcher;
for (String strLine = br.readLine(); strLine != null; strLine = br.readLine()) {
matcher = pattern.matcher(strLine);
if (matcher.find()) {
macAddr = matcher.group();
break;
}
}
} catch (IOException e) {
// e.printStackTrace();
}
return macAddr;
}
/**
* 获取系统os名称
*
* @param request
* @return
*/
public static String getOSName(HttpServletRequest request) {
String osName = null;
try {
String userAgentStr = request.getHeader("User-Agent");
UserAgent userAgent = UserAgent.parseUserAgentString(userAgentStr);
osName = userAgent.getOperatingSystem().getName();
} catch (Exception e) {
}
return osName;
}
/**
* 获取请求系统os名称
*
* @param request
* @return
*/
public static Object getOSVersion(HttpServletRequest request) {
Object osVersion = null;
try {
String userAgentStr = request.getHeader("User-Agent");
UserAgent userAgent = UserAgent.parseUserAgentString(userAgentStr);
osVersion = userAgent.getOperatingSystem().getId();
} catch (Exception e) {
}
return osVersion;
}
/**
* 获取请求浏览器名称
*
* @param request
* @return
*/
public static String getBrowserName(HttpServletRequest request) {
String name = null;
try {
String userAgentStr = request.getHeader("User-Agent");
UserAgent userAgent = UserAgent.parseUserAgentString(userAgentStr);
name = userAgent.getBrowser().getName();
} catch (Exception e) {
}
return name;
}
/**
* 获取请求浏览器版本
*
* @param request
* @return
*/
public static String getBrowserVersion(HttpServletRequest request) {
String version = null;
try {
String userAgentStr = request.getHeader("User-Agent");
UserAgent userAgent = UserAgent.parseUserAgentString(userAgentStr);
version = userAgent.getBrowserVersion().getVersion();
} catch (Exception e) {
}
return version;
}
public static String getDevTypeById(String appType) {
String name = "";
if ("1".equals(appType)) {
name = "IPAD";
} else if ("5".equals(appType)) {
name = "IPHONE";
} else if ("6".equals(appType)) {
name = "ANDROID";
}
return name;
}
public static String formatLanguageType(String languageType) {
String value = "zh";
if (!AppFrameworkUtil.isBlank(languageType)) {
if ("ja".equals(languageType)) {
value = "ja";
}
}
return value;
}
/**
* txt格式转String
*
* @param txtPath
* @return
* @throws IOException
*/
public static String txtToStr(String txtPath) throws IOException {
StringBuffer buffer = new StringBuffer();
BufferedReader bf = new BufferedReader(new InputStreamReader(new FileInputStream(txtPath), "UTF-8"));
String str = null;
while ((str = bf.readLine()) != null) {// 使用readLine方法,一次读一行
buffer.append(new String(str.getBytes(), "UTF-8"));
}
String xml = buffer.toString();
return xml;
}
public static boolean booleanUtf8(String txtPath) {
boolean flag = false;
// 原本这里的是gb2312,为了兼容更多,选择了gbk
String code = "gbk";
byte[] head = new byte[3];
try {
File file = new File(txtPath);
InputStream inputStream = new FileInputStream(file);
inputStream.read(head);
if (head[0] == -1 && head[1] == -2) {
code = "UTF-16";
} else if (head[0] == -2 && head[1] == -1) {
code = "Unicode";
} else if (head[0] == -17 && head[1] == -69 && head[2] == -65) {
code = "UTF-8";
}
inputStream.close();
//System.out.println(code);
if (code.equals("UTF-8")) {
flag = true;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return flag;
}
public static boolean booleanUtf8(File file) {
boolean flag = false;
// 原本这里的是gb2312,为了兼容更多,选择了gbk
String code = "gbk";
byte[] head = new byte[3];
try {
InputStream inputStream = new FileInputStream(file);
inputStream.read(head);
if (head[0] == -1 && head[1] == -2) {
code = "UTF-16";
} else if (head[0] == -2 && head[1] == -1) {
code = "Unicode";
} else if (head[0] == -17 && head[1] == -69 && head[2] == -65) {
code = "UTF-8";
}
inputStream.close();
//System.out.println(code);
if (code.equals("UTF-8")) {
flag = true;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return flag;
}
public static boolean upLoadFile(File file, String pdfPath) {
boolean flag = false;
File dirFile = new File(pdfPath);
try {
if (!dirFile.exists()) {
dirFile.createNewFile();
}
flag = true;
} catch (IOException e) {
e.printStackTrace();
flag = false;
}
InputStream input = null;
FileOutputStream output = null;
try {
input = new FileInputStream(file);
output = new FileOutputStream(dirFile);
byte[] bt = new byte[1024 * 1024 * 200];// 最大200m
int len = 0;
while ((len = input.read(bt)) != -1) {
output.write(bt, 0, len);
}
flag = true;
input.close();
output.flush();
output.close();
} catch (IOException e) {
flag = false;
e.printStackTrace();
}
return flag;
}
/**
* 分页查询
*
* @param request
* @param paramsMap
* @param list
* @param sqlSession
* @param string
* @return
*/
public static List