php urlencode 内容 java 输出图片

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Iterator;

import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;

public class test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String qrcode;
		try {
			//java的urldecode会指定编码,需要设置为iso-8859-1,这样输出的才是二进制
			qrcode = URLDecoder
					.decode("%89PNG%0D%0Axxxxxx",
							"ISO-8859-1");
			
			//以下为二进制内容输出为图片,不可以直接用普通方式输出图片,会导致文件头加了些东西,导致图片破坏

			ByteArrayInputStream bis = new ByteArrayInputStream(
					qrcode.getBytes());
			Iterator<?> readers = ImageIO.getImageReadersByFormatName("png");
			// ImageIO is a class containing static convenience methods for
			// locating ImageReaders
			// and ImageWriters, and performing simple encoding and decoding.

			ImageReader reader = (ImageReader) readers.next();
			Object source = bis; // File or InputStream, it seems file is OK

			ImageInputStream iis = ImageIO.createImageInputStream(source);
			// Returns an ImageInputStream that will take its input from the
			// given Object

			reader.setInput(iis, true);
			ImageReadParam param = reader.getDefaultReadParam();

			Image image = reader.read(0, param);
			// got an image file

			BufferedImage bufferedImage = new BufferedImage(
					image.getWidth(null), image.getHeight(null),
					BufferedImage.TYPE_INT_RGB);
			// bufferedImage is the RenderedImage to be written
			Graphics2D g2 = bufferedImage.createGraphics();
			g2.drawImage(image, null, null);
			File imageFile = new File("d://test.png");
			ImageIO.write(bufferedImage, "png", imageFile);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}

 

python 兼容 php urldecode 处理

#注意当前脚本是python 3.6.x
#!/usr/bin/python
# -*- coding: utf-8 -*-
import urllib,sys,re
import struct
#读取当前编码
print (sys.getdefaultencoding())
qrcode="xxxxxxxxxxxxxxxxxxxxxxxxxxx"
#php urlencode 同 python 的urlencode 差别是
#python的urldecode处理有两种 urllib.parse.unquote_plus 和 urllib.parse.unquote
#差别是 urllib.parse.unquote_plus 会将 "+" 调整为 " ".这是和php urldecode相同的方式
#但是普通urllib.parse.unquote处理会将内容进行编码,导致结果和要的冲突.文件本身已是二进制,直接读,写入就是文件,经过编码,文件就出问题

qrcode=qrcode.replace("+"," ")
#比较 plus 后的处理结果是否相同
print (urllib.parse.unquote(qrcode)==urllib.parse.unquote_plus(qrcode))
#原内容长度
print (len(qrcode))
#获取二进制内容长度,用于判断是否有漏缺
print (len(urllib.parse.unquote_to_bytes(qrcode)))
#将处理后的内容写入文件
file=open("test.png","wb")
file.write(urllib.parse.unquote_to_bytes(qrcode))
file.close();

 

新概念三册 第35课

最近公司的事多,接连两个开发离职,工作量会多些.而且事也变多了些.工作之余的时间,朋友还有各种事…时间总算是不够花了…

人一事多,除了累,就是容易堕落…能休息就休息,什么都不想干

最近抽时间把落的几篇文章都补充上来.

争取把剩下的2十几课学完,然后开始背课文,看新闻这些了.

折腾了半年…