Commit d9d25874 authored by JingChao's avatar JingChao

Deletes 附件上传/hls_plugin_weixin/bin/hls/plugin/weixin/qiye/WeiImagePostTo.class

Auto commit by GitBook Editor
parent 2f7c904f
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="lib" path="lib/json-lib-2.3-jdk15.jar"/>
<classpathentry kind="lib" path="lib/classes12.jar"/>
<classpathentry kind="lib" path="lib/commons-beanutils.jar"/>
<classpathentry kind="lib" path="lib/commons-codec-1.4.jar"/>
<classpathentry kind="lib" path="lib/commons-collections.jar"/>
<classpathentry kind="lib" path="lib/commons-httpclient-3.1.jar"/>
<classpathentry kind="lib" path="lib/commons-lang.jar"/>
<classpathentry kind="lib" path="lib/commons-logging-1.1.1.jar"/>
<classpathentry kind="lib" path="lib/ezmorph-1.0.jar"/>
<classpathentry kind="con" path="org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/Apache Tomcat v7.0"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>hls_plugin_weixin</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.6
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.6
\u8D26\u53F7
appid=wx4257809fde0f5986
\u5BC6\u7801
secret=lyJAj3vUqDg2BJp4Yc2ZLq9qwCX1rOXKV8OHSTD7JqY
\u5FAE\u4FE1\u670D\u52A1\u5668\u4FDD\u5B58\u5730\u5740
weiXinSavePath=/u01/web/JUNCHUANG_WX/attachment
\u79DF\u8D41\u4FDD\u5B58\u5730\u5740
zuLinsavePath=/u01/web/JUNCHUANG_WX/attachment
\u5FAE\u4FE1\u670D\u52A1\u5668\u8BF7\u6C42\u5730\u5740
weixinserviceUrl=http://172.20.0.115:5371/junchuang_wx/WeixinServlet
file_type_code=image
\u79DF\u8D41\u8BF7\u6C42\u5730\u5740
zuLinserviceUrl=http://172.20.0.115:5371/junchuang_wx/ZuLinServlet
\ No newline at end of file
oracleDriver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@172.20.0.115:1521:hls
username=jc_hls
password=jc_hls
\ No newline at end of file
package hls.plugin.weixin.common;
import hls.plugin.weixin.qiye.DBUtil;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
public class GetPropertiesValue
{
public Map<String, String> GetValue()
{
Map map = new HashMap();
Properties prop = new Properties();
InputStream in = null;
in = new DBUtil().getPathName("hls/plugin/weixin/qiye/constants.properties");
try {
prop.load(in);
map.put("appid", prop.getProperty("appid"));
map.put("secret", prop.getProperty("secret"));
map.put("winXinSavePath", prop.getProperty("weiXinSavePath"));
map.put("zuLinsavePath", prop.getProperty("zuLinsavePath"));
map.put("weixinserviceUrl", prop.getProperty("weixinserviceUrl"));
map.put("zuLinserviceUrl", prop.getProperty("zuLinserviceUrl"));
map.put("file_type_code", prop.getProperty("file_type_code"));
} catch (Exception e) {
e.printStackTrace();
if (in != null)
try {
in.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
finally
{
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return map;
}
}
\ No newline at end of file
package hls.plugin.weixin.common;
import java.io.IOException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
public class HttpUtil
{
public static String httpRequestUrl(String requestUrl, String method)
{
HttpClient client = new HttpClient();
HttpMethod httpMethod = null;
String responseMessage = "";
if ("GET".equals(method))
httpMethod = new GetMethod(requestUrl);
else {
httpMethod = new PostMethod(requestUrl);
}
try
{
client.executeMethod(httpMethod);
} catch (HttpException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
try
{
responseMessage = httpMethod.getResponseBodyAsString();
httpMethod.releaseConnection();
} catch (IOException e) {
e.printStackTrace();
}
return responseMessage;
}
}
\ No newline at end of file
package hls.plugin.weixin.fuwu;
import hls.plugin.weixin.common.GetPropertiesValue;
import hls.plugin.weixin.common.HttpUtil;
import java.util.Map;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
public class GetOpenid
{
public static String getAccessTokenURL(String code, String appid, String secret)
{
String mutiurl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appid + "&secret=" + secret + "&code=" + code + "&grant_type=authorization_code";
return mutiurl;
}
public String getPenid(String code) {
String openid = "";
GetPropertiesValue gp = new GetPropertiesValue();
String requestUrl = getAccessTokenURL(code, (String)gp.GetValue().get("appid"), (String)gp.GetValue().get("secret"));
JSONObject jsonObject = JSONObject.fromObject(HttpUtil.httpRequestUrl(
requestUrl, "GET"));
if (jsonObject != null) {
try {
openid = jsonObject.getString("openid");
} catch (JSONException e) {
openid = null;
}
}
return openid;
}
}
\ No newline at end of file
package hls.plugin.weixin.fuwu;
import hls.plugin.weixin.common.HttpUtil;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
public class ServiceUtil
{
public static String getAccessCodeURL(String appid, String redirect_uri, String state)
{
String mutiurl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid + "&redirect_uri=" + redirect_uri + "&response_type=code&scope=snsapi_base&state=" + state + "#wechat_redirect";
return mutiurl;
}
public static void getAccessCodeRuquest(String appid, String redirect_uri, String state)
{
String redirect_urito = "";
try {
redirect_urito = URLEncoder.encode(redirect_uri, "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String requestUrl = getAccessCodeURL(appid, redirect_urito, state);
HttpUtil.httpRequestUrl(requestUrl, "GET");
}
}
\ No newline at end of file
package hls.plugin.weixin.qiye;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public class DBUtil
{
private static String oracleDriver;
private static String url;
private static String username;
private static String password;
static
{
try
{
get();
Class.forName(oracleDriver);
} catch (ClassNotFoundException e) {
System.out.println("..");
}
}
public InputStream getPathName(String pathName)
{
return DBUtil.class.getClassLoader().getResourceAsStream(pathName);
}
private static void get() {
Properties prop = new Properties();
InputStream in = null;
try {
in = new DBUtil().getPathName("hls/plugin/weixin/qiye/data_jdbc.properties");
prop.load(in);
oracleDriver = prop.getProperty("oracleDriver");
url = prop.getProperty("url");
username = prop.getProperty("username");
password = prop.getProperty("password");
} catch (Exception e) {
e.printStackTrace();
if (in != null)
try {
in.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
finally
{
if (in != null)
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static Connection getConnection()
throws SQLException
{
return DriverManager.getConnection(url, username, password);
}
public static void main(String[] args) {
try {
getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
\ No newline at end of file
package hls.plugin.weixin.qiye;
import java.io.PrintStream;
public class Ertrt
{
public static void main(String[] args)
{
String time = (long)(Math.random() * 10000000.0D)+"";
System.out.println(time);
}
}
\ No newline at end of file
package hls.plugin.weixin.qiye;
public class FndAtmAttachment
{
private String source_pk_value;
private String file_type_code;
private String mime_type;
private String file_name;
private int file_size;
private String file_path;
private int user_id;
private String table_name;
private String table_pk_value;
public FndAtmAttachment()
{
}
public FndAtmAttachment(String source_pk_value, String file_type_code, String mime_type, String file_name, int file_size, String file_path, int user_id, String table_name, String table_pk_value)
{
this.source_pk_value = source_pk_value;
this.file_type_code = file_type_code;
this.mime_type = mime_type;
this.file_name = file_name;
this.file_size = file_size;
this.file_path = file_path;
this.user_id = user_id;
this.table_name = table_name;
this.table_pk_value = table_pk_value;
}
public String getSource_pk_value() {
return this.source_pk_value;
}
public void setSource_pk_value(String source_pk_value) {
this.source_pk_value = source_pk_value;
}
public String getFile_type_code() {
return this.file_type_code;
}
public void setFile_type_code(String file_type_code) {
this.file_type_code = file_type_code;
}
public String getMime_type() {
return this.mime_type;
}
public void setMime_type(String mime_type) {
this.mime_type = mime_type;
}
public String getFile_name() {
return this.file_name;
}
public void setFile_name(String file_name) {
this.file_name = file_name;
}
public int getFile_size() {
return this.file_size;
}
public void setFile_size(int file_size) {
this.file_size = file_size;
}
public String getFile_path() {
return this.file_path;
}
public void setFile_path(String file_path) {
this.file_path = file_path;
}
public int getUser_id() {
return this.user_id;
}
public void setUser_id(int user_id) {
this.user_id = user_id;
}
public String getTable_name() {
return this.table_name;
}
public void setTable_name(String table_name) {
this.table_name = table_name;
}
public String getTable_pk_value() {
return this.table_pk_value;
}
public void setTable_pk_value(String table_pk_value) {
this.table_pk_value = table_pk_value;
}
}
\ No newline at end of file
package hls.plugin.weixin.qiye;
public abstract interface IWeChatDao
{
public abstract boolean save(FndAtmAttachment paramFndAtmAttachment);
}
\ No newline at end of file
package hls.plugin.weixin.qiye;
import hls.plugin.weixin.common.GetPropertiesValue;
import hls.plugin.weixin.common.HttpUtil;
import java.util.Map;
public class RequestToCompany
{
public static void sendMassage(String[] mediaId, String table_pk_value, String userId, String tableName, String pck_id)
{
GetPropertiesValue gp = new GetPropertiesValue();
for (int i = 0; i < mediaId.length; i++) {
String requestUrl = (String)gp.GetValue().get("weixinserviceUrl") + "?mediaId=" + mediaId[i] + "&pck_id=" + pck_id + "&userId=" + userId + "&tableName=" + tableName + "&table_pk_value=" + table_pk_value;
HttpUtil.httpRequestUrl(requestUrl, "GET");
}
}
public static void main(String[] args)
{
}
}
\ No newline at end of file
package hls.plugin.weixin.qiye;
import hls.plugin.weixin.common.HttpUtil;
import java.io.PrintStream;
import java.util.Map;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
public class WXupAndDown
{
public static String getJSSDKAccessToken(String acceToken_urlx)
{
String returnString = "";
String requestUrl = acceToken_urlx;
JSONObject jsonObject = JSONObject.fromObject(HttpUtil.httpRequestUrl(
requestUrl, "GET"));
if (jsonObject != null) {
try {
returnString = jsonObject.getString("access_token");
} catch (JSONException e) {
returnString = null;
}
}
return returnString;
}
public static String getJSSDKTicket(String access_token, String ticket_urlx)
{
String returnString = "";
String requestUrl = ticket_urlx.replace("ACCESS_TOKEN", access_token);
JSONObject jsonObject = JSONObject.fromObject(HttpUtil.httpRequestUrl(
requestUrl, "GET"));
if (jsonObject != null) {
try {
returnString = jsonObject.getString("ticket");
} catch (JSONException e) {
returnString = null;
}
}
return returnString;
}
public static Map<String, String> getSignture(String id, String secrect, String url)
{
String acceToken_urlx = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + id + "&corpsecret=" + secrect;
String ticket_urlx = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=ACCESS_TOKEN";
String js_accessToken = getJSSDKAccessToken(acceToken_urlx);
String jsapi_ticket = getJSSDKTicket(js_accessToken, ticket_urlx);
Map map = WxJSsign.sign(jsapi_ticket, url);
return map;
}
public static void main(String[] args) {
String id = "wx4257809fde0f5986";
String secrect = "iTgSpAaEYejcyPBLlyikzx6kvSVuiMBxotfAkAdWAKo6nF8cefdJSTgLXqivXpZ7";
String url = "http://cmsplus.com.cn";
Map map = getSignture(id, secrect, url);
System.out.println("1" + (String)map.get("nonceStr"));
System.out.println("2" + (String)map.get("timestamp"));
System.out.println("3" + (String)map.get("signature"));
}
}
\ No newline at end of file
package hls.plugin.weixin.qiye;
import java.io.PrintStream;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
public class WeCatDaoImpl
implements IWeChatDao
{
public static void main(String[] args)
{
FndAtmAttachment fnd = new FndAtmAttachment("1", "1", "1", "1", 1, "1", 1, "1", "1");
boolean ss = new WeCatDaoImpl().save(fnd);
System.out.println(ss);
}
public boolean save(FndAtmAttachment fnd) {
boolean success = false;
Connection conn = null;
CallableStatement pst = null;
try {
conn = DBUtil.getConnection();
pst = conn
.prepareCall("{ call fnd_atm_attachment_all_pkg.insert_fnd_atm_attachment(?,?,?,?,?,?,?,?,?,?) }");
pst.registerOutParameter(1, 4);
pst.setString(2, fnd.getSource_pk_value());
pst.setString(3, fnd.getFile_type_code());
pst.setString(4, fnd.getMime_type());
pst.setString(5, fnd.getFile_name());
pst.setInt(6, fnd.getFile_size());
pst.setString(7, fnd.getFile_path());
pst.setInt(8, fnd.getUser_id());
pst.setString(9, fnd.getTable_name());
pst.setString(10, fnd.getTable_pk_value());
pst.execute();
conn.commit();
success = true;
} catch (SQLException e) {
e.printStackTrace();
try
{
if (pst != null)
pst.close();
if (conn != null)
conn.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
finally
{
try
{
if (pst != null)
pst.close();
if (conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return success;
}
}
\ No newline at end of file
package hls.plugin.weixin.qiye;
import hls.plugin.weixin.common.GetPropertiesValue;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
public class WeiImagePostTo
{
public void send(String mediaId, String pck_id, String fileName, String filetype, String fileSize, String userId, String tableName, String table_pk_value)
{
GetPropertiesValue gp = new GetPropertiesValue();
HttpURLConnection con = null;
OutputStream out = null;
InputStream in = null;
InputStream inputStream = null;
ByteArrayOutputStream baos = null;
int length = 0;
try {
File file = new File((String)gp.GetValue().get("winXinSavePath") + "/" +
fileName + "." + filetype);
if ((!file.exists()) || (!file.isFile())) {
throw new IOException("ļ");
}
String enCodemediaId = mediaId;
/*URL urlObj = new URL((String)gp.GetValue().get("zuLinserviceUrl") +
"?mediaId=" + enCodemediaId + "&pck_id=" + pck_id +
"&fileName=" + fileName + "&filetype=" + filetype +
"&fileSize=" + fileSize + "&userId=" + userId + "&tableName=" + tableName + "&table_pk_value=" + table_pk_value);
*/
int userId2 = Integer.parseInt(userId);
int file_size_int = Integer.parseInt(fileSize);
String mime_type = (String)gp.GetValue().get("file_type_code") + "/" +filetype;
String filepath =
(String)gp.GetValue()
.get("weiXinSavePath") +
"/" + fileName;
FndAtmAttachment fnd = new FndAtmAttachment(table_pk_value, filetype, mime_type, fileName, file_size_int, filepath, userId2, tableName, pck_id);
boolean ss = new WeCatDaoImpl().save(fnd);
System.out.println(ss);
/* con = (HttpURLConnection)urlObj.openConnection();
con.setDoOutput(true);
con.setRequestProperty("Content-Length", fileSize);
con.setRequestProperty("Content-type", "application/image");
con.setRequestMethod("POST");
con.connect();
out = con.getOutputStream();
in = new FileInputStream(file);
int bytes = -1;
byte[] bufferOut = new byte[1024];
while ((bytes = in.read(bufferOut)) != -1) {
out.write(bufferOut, 0, bytes);
}
inputStream = con.getInputStream();*/
}
catch (Exception e) {
System.out.println("쳣:" + e.getMessage());
if (in != null) {
try {
in.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (out != null) {
try {
out.flush();
out.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (con != null) {
con.disconnect();
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (baos != null)
try {
baos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
finally
{
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (out != null) {
try {
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (con != null) {
con.disconnect();
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (baos != null)
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
\ No newline at end of file
package hls.plugin.weixin.qiye;
import hls.plugin.weixin.common.HttpUtil;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
public class WeiImageUpload
{
public JSONObject send(String fileType, String filePath, String id, String secret)
throws Exception
{
String result = null;
File file = new File(filePath);
if ((!file.exists()) || (!file.isFile())) {
throw new IOException("文件不存在");
}
String token = getNowToken(id, secret);
URL urlObj = new URL("https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=" + token +
"&type=" + fileType);
HttpURLConnection con = (HttpURLConnection)urlObj.openConnection();
con.setRequestMethod("POST");
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Charset", "UTF-8");
String BOUNDARY = "----------" + System.currentTimeMillis();
con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
StringBuilder sb = new StringBuilder();
sb.append("--");
sb.append(BOUNDARY);
sb.append("\r\n");
sb.append("Content-Disposition: form-data;name=\"media\";filename=\"" + file.getName() + "\"\r\n");
sb.append("Content-Type:application/octet-stream\r\n\r\n");
byte[] head = sb.toString().getBytes("utf-8");
OutputStream out = new DataOutputStream(con.getOutputStream());
out.write(head);
DataInputStream in = new DataInputStream(new FileInputStream(file));
int bytes = 0;
byte[] bufferOut = new byte[1024];
while ((bytes = in.read(bufferOut)) != -1) {
out.write(bufferOut, 0, bytes);
}
in.close();
byte[] foot = ("\r\n--" + BOUNDARY + "--\r\n").getBytes("utf-8");
out.write(foot);
out.flush();
out.close();
StringBuffer buffer = new StringBuffer();
BufferedReader reader = null;
try
{
reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null)
{
buffer.append(line);
}
if (result == null)
result = buffer.toString();
}
catch (IOException e) {
System.out.println("发送POST请求出现异常!" + e);
e.printStackTrace();
throw new IOException("数据读取异常");
} finally {
if (reader != null) {
reader.close();
}
}
JSONObject jsonObj = JSONObject.fromObject(result);
System.out.println("jsonObj>>>>" + jsonObj);
return jsonObj;
}
public static String getNowToken(String id, String secret) {
String returnString = "";
String requestUrl = getNowTokenURL(id, secret);
JSONObject jsonObject = JSONObject.fromObject(HttpUtil.httpRequestUrl(
requestUrl, "GET"));
if (jsonObject != null) {
try {
returnString = jsonObject.getString("access_token");
} catch (JSONException e) {
returnString = null;
}
}
return returnString;
}
public static String getNowTokenURL(String id, String secret) {
String mutiurl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + id + "&corpsecret=" + secret;
return mutiurl;
}
public static void main(String[] args) {
try {
JSONObject jsonObj = new WeiImageUpload().send("image", "C:/Users/Administrator/Desktop/pak/3.jpg", "wx4257809fde0f5986", "iTgSpAaEYejcyPBLlyikzx6kvSVuiMBxotfAkAdWAKo6nF8cefdJSTgLXqivXpZ7");
System.out.println(jsonObj.getString("media_id"));
System.out.println(jsonObj.getString("created_at"));
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static Map<String, String> weiUpload(String fileType, String filePath, String id, String secret)
{
Map map = new HashMap();
try {
JSONObject jsonObj = new WeiImageUpload().send(fileType, filePath, id, secret);
map.put("media_id", jsonObj.getString("media_id"));
map.put("created_at", jsonObj.getString("created_at"));
}
catch (Exception e) {
e.printStackTrace();
}
return map;
}
}
\ No newline at end of file
package hls.plugin.weixin.qiye;
import hls.plugin.weixin.common.HttpUtil;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
public class WeixinDownload
{
static WeixinDownload weixinMedia = null;
public static String downloadMedia(String accessToken, String mediaId, String savePath, String imageName)
{
String filePath = null;
String filetype = "";
HttpURLConnection conn = null;
BufferedInputStream bis = null;
FileOutputStream fos = null;
String requestUrl = "https://qyapi.weixin.qq.com/cgi-bin/media/get?access_token=ACCESS_TOKEN&media_id=MEDIA_ID";
requestUrl = requestUrl.replace("ACCESS_TOKEN", accessToken).replace("MEDIA_ID", mediaId);
try {
URL url = new URL(requestUrl);
conn = (HttpURLConnection)url.openConnection();
conn.setDoInput(true);
conn.setRequestMethod("GET");
if (!savePath.endsWith("/")) {
savePath = savePath + "/";
}
String fileExt = WeixinUtil.getTypeByStream(conn.getHeaderField("Content-Type"));
filePath = savePath + imageName + "." + fileExt.substring(fileExt.indexOf("/") + 1);
filetype = fileExt.substring(fileExt.indexOf("/") + 1);
bis = new BufferedInputStream(conn.getInputStream());
fos = new FileOutputStream(new File(filePath));
byte[] buf = new byte[1024];
int size = 0;
while ((size = bis.read(buf)) != -1)
fos.write(buf, 0, size);
}
catch (Exception e) {
filePath = null;
String error = String.format("ýļʧ", new Object[] { e });
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (conn != null) {
conn.disconnect();
}
}
return filetype;
}
public static String getNowToken(String id, String secret) {
String returnString = "";
String requestUrl = getNowTokenURL(id, secret);
JSONObject jsonObject = JSONObject.fromObject(HttpUtil.httpRequestUrl(
requestUrl, "GET"));
if (jsonObject != null) {
try {
returnString = jsonObject.getString("access_token");
} catch (JSONException e) {
returnString = null;
}
}
return returnString;
}
public static String getNowTokenURL(String id, String secret) {
String mutiurl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + id + "&corpsecret=" + secret;
return mutiurl;
}
public static void main(String[] args) {
weiDownload("wx4257809fde0f5986", "iTgSpAaEYejcyPBLlyikzx6kvSVuiMBxotfAkAdWAKo6nF8cefdJSTgLXqivXpZ7", "1qyU7RGN3LHdsMrqCw7VrTc85wY-Q5Z3pEk_VJKcU9E3Nx_ukMH7ik2Vp_43EiMa6VyEjpNwtuEQ2ILqoe7F5cA", "G:/pad/");
}
private static String createImageName() {
return (long)(Math.random()*10000000)+"";
// return Long.toString(System.currentTimeMillis() / 1000);
}
public static Map<String, String> weiDownload(String id, String secret, String mediaId, String savePath) {
Map map = new HashMap();
String accessToken = getNowToken(id, secret);
String imageName = createImageName();
String filetype = downloadMedia(accessToken, mediaId, savePath, imageName);
String fileSize=getFileSize(savePath,imageName,filetype)+"";
map.put("fileName", imageName);
map.put("filetype", filetype);
map.put("fileSize", fileSize);
return map;
}
public static long getFileSize(String savePath, String imageName, String filetype) {
long fileSize = 0L;
File file = new File(savePath + "/" + imageName + "." + filetype);
if ((file.exists()) && (file.isFile())) {
fileSize = file.length() / 1024L;
}
return fileSize;
}
}
\ No newline at end of file
package hls.plugin.weixin.qiye;
import hls.plugin.weixin.common.GetPropertiesValue;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class WeixinServlet extends HttpServlet
{
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
GetPropertiesValue gp = new GetPropertiesValue();
System.out.println("11111");
String mediaId = request.getParameter("mediaId");
String pck_id = request.getParameter("pck_id");
String id = (String)gp.GetValue().get("appid");
String secret = (String)gp.GetValue().get("secret");
String winXinSavePath = (String)gp.GetValue().get("winXinSavePath");
String userId = request.getParameter("userId");
String tableName = request.getParameter("tableName");
String table_pk_value = request.getParameter("table_pk_value");
new WeixinDownload(); Map map = WeixinDownload.weiDownload(id, secret, mediaId, winXinSavePath);
String fileName = (String)map.get("fileName");
String filetype = (String)map.get("filetype");
String fileSize = (String)map.get("fileSize");
try {
new WeiImagePostTo().send(mediaId, pck_id, fileName, filetype, fileSize, userId, tableName, table_pk_value);
} catch (Exception e) {
e.printStackTrace();
}
PrintWriter pw = response.getWriter();
pw.println("OK!!");
pw.flush();
pw.close();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
\ No newline at end of file
package hls.plugin.weixin.qiye;
public class WeixinUtil
{
public static String getTypeByStream(String cot)
{
String type = cot.toUpperCase();
if (type.contains("FFD8FF"))
return "jpg";
if (type.contains("89504E47"))
return "png";
if (type.contains("47494638"))
return "gif";
if (type.contains("49492A00"))
return "tif";
if (type.contains("424D")) {
return "bmp";
}
return type.toLowerCase();
}
}
\ No newline at end of file
package hls.plugin.weixin.qiye;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Formatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
public class WxJSsign
{
public static Map<String, String> sign(String jsapi_ticket, String url)
{
Map ret = new HashMap();
String nonce_str = create_nonce_str();
String timestamp = create_timestamp();
String signature = "";
String string1 = "jsapi_ticket=" + jsapi_ticket + "&noncestr=" + nonce_str + "&timestamp=" + timestamp + "&url=" + url;
try {
MessageDigest crypt = MessageDigest.getInstance("SHA-1");
crypt.reset();
crypt.update(string1.getBytes("UTF-8"));
signature = byteToHex(crypt.digest());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
ret.put("url", url);
ret.put("jsapi_ticket", jsapi_ticket);
ret.put("nonceStr", nonce_str);
ret.put("timestamp", timestamp);
ret.put("signature", signature);
return ret;
}
private static String create_nonce_str() {
return UUID.randomUUID().toString();
}
private static String create_timestamp() {
return Long.toString(System.currentTimeMillis() / 1000L);
}
private static String byteToHex(byte[] hash) {
Formatter formatter = new Formatter();
byte[] arrayOfByte = hash; int j = hash.length; for (int i = 0; i < j; i++) { byte b = arrayOfByte[i];
formatter.format("%02x", new Object[] { Byte.valueOf(b) });
}
String result = formatter.toString();
formatter.close();
return result;
}
}
\ No newline at end of file
package hls.plugin.weixin.qiye;
import hls.plugin.weixin.common.GetPropertiesValue;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ZuLinServlet extends HttpServlet
{
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
InputStream in = null;
OutputStream out = null;
GetPropertiesValue gp = new GetPropertiesValue();
String pck_id = request.getParameter("pck_id");
String file_name = request.getParameter("fileName") + "." + request.getParameter("filetype");
String file_size = request.getParameter("fileSize");
String userId = request.getParameter("userId");
int userId2 = Integer.parseInt(userId);
String tableName = request.getParameter("tableName");
String table_pk_value = request.getParameter("table_pk_value");
int file_size_int = Integer.parseInt(file_size);
String file_type_code = request.getParameter("filetype");
String mime_type = (String)gp.GetValue().get("file_type_code") + "/" + request.getParameter("filetype");
String filepath =
(String)gp.GetValue()
.get("zuLinsavePath") +
"/" + file_name;
System.out.println("filepath>>>" + filepath);
try {
in = request.getInputStream();
out = new FileOutputStream(filepath);
int bytes = -1;
byte[] bufferOut = new byte[1024];
while ((bytes = in.read(bufferOut)) != -1)
out.write(bufferOut, 0, bytes);
}
catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.flush();
out.close();
}
}
FndAtmAttachment fnd = new FndAtmAttachment(table_pk_value, file_type_code, mime_type, file_name, file_size_int, filepath, userId2, tableName, pck_id);
boolean ss = new WeCatDaoImpl().save(fnd);
System.out.println(ss);
}
}
\ No newline at end of file
\u8D26\u53F7
appid=wx4257809fde0f5986
\u5BC6\u7801
secret=lyJAj3vUqDg2BJp4Yc2ZLq9qwCX1rOXKV8OHSTD7JqY
\u5FAE\u4FE1\u670D\u52A1\u5668\u4FDD\u5B58\u5730\u5740
weiXinSavePath=/u01/web/JUNCHUANG_WX/attachment
\u79DF\u8D41\u4FDD\u5B58\u5730\u5740
zuLinsavePath=/u01/web/JUNCHUANG_WX/attachment
\u5FAE\u4FE1\u670D\u52A1\u5668\u8BF7\u6C42\u5730\u5740
weixinserviceUrl=http://172.20.0.115:5371/junchuang_wx/WeixinServlet
file_type_code=image
\u79DF\u8D41\u8BF7\u6C42\u5730\u5740
zuLinserviceUrl=http://172.20.0.115:5371/junchuang_wx/ZuLinServlet
\ No newline at end of file
oracleDriver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@172.20.0.115:1521:hls
username=jc_hls
password=jc_hls
\ No newline at end of file
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