• 大小: 8KB
    文件类型: .java
    金币: 2
    下载: 1 次
    发布日期: 2021-06-17
  • 语言: Java
  • 标签:

资源简介

getwxacodeunlimit 生成二维码 并保存在本地 。获取到access_token 可以直接使用

资源截图

代码片段和文件信息

package com.wangmai.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import net.sf.json.JSONobject;

public class WeiXinQRManager {
/**
 * @param reqUrl
 *            基础的url地址
 * @param params
 *            查询参数
 * @return 构建好的url
 */

public static String httpPostWithJSON(HttpServletRequest request String url String json String id)
throws Exception {
String result = null;
// 将JSON进行UTF-8编码以便传输中文
String encoderJson = URLEncoder.encode(json HTTP.UTF_8);
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader(HTTP.CONTENT_TYPE “application/json“);
StringEntity se = new StringEntity(json);
se.setContentType(“application/json“);
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE “UTF-8“));
httpPost.setEntity(se);
// httpClient.execute(httpPost);
HttpResponse response = httpClient.execute(httpPost);
if (response != null) {
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
InputStream instreams = resEntity.getContent();
// ResourceBundle systemConfig =
// ResourceBundle.getBundle(“config/system“
// Locale.getDefault());
// String uploadSysUrl =
// systemConfig.getString(“agentImgUrl“)+id+“/“;
// File saveFile = new File(uploadSysUrl+id+“.jpg“);
String uploadSysUrl = “D:\\upload“ + “/“;
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd“);
String dateStr = sdf.format(new Date());
String xdPath = “/upload/“ + dateStr;
String filePath = request.getServletContext().getRealPath(xdPath);// 保存文件路径
File saveFile = new File(filePath);
// 判断这个文件(saveFile)是否存在
if (!saveFile.exists()) {
// 如果不存在就创建这个文件夹
saveFile.mkdirs();
}
String newHeadImgName = ““;// 重新设置要保存头像的文件名
// 获取当前时间
Date d = new Date();
newHeadImgName += ““ + d.getTime() + ((int) (Math.random() * 9000 + 1000)) + “.jpg“;
saveToImgByInputStream(instreams filePath newHeadImgName);
result = xdPath + “/“ + newHeadImgName;
}
}
return result;
}

/*
 * @param instreams 二进制流
 * 
 * @param imgPath 图片的保存路径
 * 
 * @param imgName 图片的名称
 * 
 * @return 1:保存正常 0:保存

评论

共有 条评论