SSM中使用base64进行图片的上传

1.地址

http://imgbase64.duoshitong.com/

图片转换为base64,就是一个很长的String

2.保存

a.使用postman进行测试

入参:

data:{"files":["sssssss"."sdadsadasda"]}

代码保存

@RequestMapping(value = "suggestion", method = {RequestMethod.POST})
    public ResultData addSuggestion(
            @RequestBody String data)

    {
        
        List<String> urls = new LinkedList<String>();
        //文件的保存路径
        String savePath = saveurl.replace("//", "\\")+fileurl.replace("/", "\\")+"\\";
        try {
            
            
            //File filedir = new File("D:\\gwxStatic\\static");
            
            //如果文件夹不存在,则创建文件夹
            File filedir = new File(saveurl.replace("//", "\\\\")+fileurl.replace("/", "\\\\"));
            if(!filedir.exists()){
                filedir.mkdirs();                
            }    
        
            
            JSONObject para = JSONObject.parseObject(data);
            JSONObject jb = para.getJSONObject("data");      
            
            JSONArray base64StringArray =  jb.getJSONArray("files");
            
            
             //拿到base64的字符串数组
            String[] array = base64StringArray.toString().split(",");
            
            
            //解析并保存文件
            for (int i = 0; i < array.length; i++) {
                                
                if(i%2!=0){
                    String uuid = UUID.randomUUID().toString();
                    base64CodeToimage(array[i],savePath+uuid+".jpg");
                    urls.add(uuid+".jpg");
                }                                                
            }           

/**
     * base64字符串转换为图片,并保存
     * @Description
     * @Param
     * @Return
     */
    public static void base64CodeToimage(String basee64code,String url) {
        
        BASE64Decoder bd = new BASE64Decoder();
        
        try {

            File file = new File(url);
            FileOutputStream fo = new FileOutputStream(file);
            fo.write(bd.decodeBuffer(basee64code));
            fo.flush();
            fo.close();
        } catch (IOException e) {        
            String code = UUID.randomUUID().toString();
            log.error(code, e);
            log.error("保存图片失败");
        }
    }

参考

package other;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * @Description
 * @Author zengzhiqiang
 * @Date 2018年7月20日 
 */

public class test1 {

	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		/*String base64code = getImageBase64code("E:\\apache-tomcat-7.0.85\\webapps\\onis-upgrade\\"
				+ "uploaddata\\system\\photo\\201611\\201611132020381479039638193s.jpg");*/
		
		String base64code = getImageBase64code("D:\\gwxStatic\\static\\00b50c62-df09-4bfd-8740-b1c04d6875b2.png");
		
		
		
		
		
		base64CodeToimage(base64code);
		
		System.out.println("ok");
	}
	
	
	public static String getImageBase64code(String iamgePath) {
		try {
			File file = new File(iamgePath);
			
			FileInputStream fs;

			fs = new FileInputStream(file);

			byte[] data = new byte[fs.available()];
			fs.read(data);
			fs.close();
			BASE64Encoder be = new BASE64Encoder();

			return be.encode(data);
		} catch (FileNotFoundException e) {

			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	public static void base64CodeToimage(String basee64code) {
			
		BASE64Decoder bd = new BASE64Decoder();
		
		try {

			File file = new File(
					"D:\\gwxStatic\\test24.jpg");
			FileOutputStream fo = new FileOutputStream(file);
			fo.write(bd.decodeBuffer(basee64code));
			fo.flush();
			fo.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

猜你喜欢

转载自blog.csdn.net/zzqtty/article/details/81130915
今日推荐