app接口上传图片(通过转码为Base64格式字符串上传)

app开发中接口有时会涉及到图片上传,代码如下

接受参数

 			//上传
 			String imgName = this.getRequestBase64Img("headImg");

方法

    /**
     * 上传图片(前台为Base64格式)
     * @param fileName 上传图片的属性名
     * @return
     * @throws Exception
     */
    public String getRequestBase64Img(String fileName) throws Exception{
    	return getRequestBase64File(fileName, Constant.SJYS_IMG);//ROOT_PATH + "/SJYS_IMG/"
    }

具体方法

    /**
     * 上传文件(前台为Base64格式)
     * @param fileName 上传文件的名字(前台的属性名称)
     * @param filePath 文件保存的目标路径
     * @return
     * @throws Exception
     */
    public String getRequestBase64File(String fileName, String filePath) throws Exception{
    	//得到前台传递过来的Base64格式的字符串
    	String fileStr = this.getRequest().getParamValue(fileName);
    	if (fileStr == null){
    		return null;
    	}
    	
    	if(filePath == null || "".equals(filePath)){
			filePath = Constant.XLS_TEMP;//ROOT_PATH + "/uploadTemplates/";
		}
    	
    	//前台生成base64时,是以   ABCDEFWWFE.jpg 这种格式传过来的 .jpg是文件的后缀名
		String suffix = fileStr.substring(fileStr.lastIndexOf("."));  //后缀名
		fileStr = fileStr.substring(0,fileStr.lastIndexOf(".")); //文件的真正base64内容
		
		//产生的文件名称
		String name = getRandomFileName()+suffix;
		
		BASE64Decoder decoder = new BASE64Decoder();
		try {
			// Base64解码
			byte[] b = decoder.decodeBuffer(fileStr);
			for (int i = 0; i < b.length; ++i) {
				if (b[i] < 0) {// 调整异常数据
					b[i] += 256;
				}
			}
			// 生成文件
			OutputStream out = new FileOutputStream(filePath+name);
			out.write(b);
			out.flush();
			out.close();
			return name;
		} catch (Exception e) {
			return null;
		}
	}
    /**
	 * 生成随机名字
	 */
	public String getRandomFileName() {
		Random r = new Random();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmssSSS");
		StringBuffer sb = new StringBuffer();
		sb.append(r.nextInt(100));
		sb.append(r.nextInt(100));
		sb.append("_");
		sb.append(sdf.format(new Date()));
		sb.append("_");
		sb.append(r.nextInt(100));
		sb.append(r.nextInt(100));
		return sb.toString();
	}



在C#中,使用AppKey和AppSecret获取accessToken并生成Base64编码的字符串通常是在进行OAuth2.0授权流程时的一种做法。这里假设你正在构建一个服务客户,需要向某个API服务器请求访问令牌。下面是基本步骤: 1. 首先,你需要提供你的应用程序密钥(AppKey)和秘密(AppSecret),这些信息通常是API提供商给你的,用于身份验证。 ```csharp string appId = "your_app_id"; string appSecret = "your_app_secret"; ``` 2. 创建一个POST请求的数据模型,通常包括键值对`grant_type="client_credentials"`和`scope`(如果有特定的权限范围)。例如: ```csharp Dictionary<string, string> parameters = new Dictionary<string, string> { { "grant_type", "client_credentials" }, { "scope", "your_scope" } // 根据API文档填写 }; ``` 3. 然后,将AppKey和AppSecret与请求参数一起发送到API提供的token endpoint,通常格式为`https://api.example.com/oauth/token`。你可以使用HttpClient或其他HTTP库来发送此请求。 ```csharp using (var httpClient = new HttpClient()) { var content = new FormUrlEncodedContent(parameters); var response = await httpClient.PostAsync("https://api.example.com/oauth/token", content); if (!response.IsSuccessStatusCode) throw new Exception($"Failed to get access token, status code: {response.StatusCode}"); } ``` 4. 获取到响应后,解析返回的内容以获取accessToken。这通常是一个JSON响应,包含`access_token`字段。假设你得到了`accessToken`: ```csharp string accessToken = response.Content.ReadAsStringAsync().Result; ``` 5. 最后,将AppKey和AppSecret与accessToken组合,然后进行Base64编码。注意,为了安全,实际应用中应处理这些敏感信息,可能将其存储在一个安全的地方而不是明文操作: ```csharp string secretInfo = $"{appId}:{appSecret}"; byte[] secretBytes = Encoding.UTF8.GetBytes(secretInfo); string encodedSecret = Convert.ToBase64String(secretBytes); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值