base64文件上传

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import sun.misc.BASE64Decoder;

import javax.annotation.Resource;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.UUID;

/**
 * @author huqi 2021/8/12
 */
@Slf4j
@Configuration
public class UploadFile {

    @Resource
    private OpenAuthConfig openAuthConfig;

    /**
     * base64文件上传
     * @param actionUrl 请求地址
     * @param base64File base64文件
     * @param type 文件扩展名
     * @param token token
     * @return
     * @throws IOException
     */
    public String upLoadFilePost(String actionUrl,String base64File,String type, String token) throws IOException {
        String tempFileName = UUID.randomUUID()+"."+type;
        BASE64Decoder decoder = new BASE64Decoder();
        base64File = base64File.replaceAll(" ", "+");
        byte[] bfile = decoder.decodeBuffer(base64File.replace("data:image/"+type+";base64,", ""));
        for (int i = 0; i < bfile.length; ++i) {
            if (bfile[i] < 0) {
                bfile[i] += 256;
            }
        }
        actionUrl = openAuthConfig.getDomainName() + actionUrl;
        log.info("post地址"+actionUrl);
        String BOUNDARY = java.util.UUID.randomUUID().toString();
        String PREFIX = "--", LINEND = "\r\n";
        String MULTIPART_FROM_DATA = "multipart/form-data";
        String CHARSET = "UTF-8";
        URL uri = new URL(actionUrl);
        HttpURLConnection conn = (HttpURLConnection) uri.openConnection();
        conn.setReadTimeout(5 * 1000);
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setUseCaches(false);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("connection", "keep-alive");
        conn.setRequestProperty("Charsert", "UTF-8");
        conn.setRequestProperty("Content-Type", MULTIPART_FROM_DATA
                + ";boundary=" + BOUNDARY);
        conn.setRequestProperty(IOpenApi.BASIC_HEADER_TOKEN, IOpenApi.TOKEN_BEARER + token);
        DataOutputStream outStream = new DataOutputStream(
                conn.getOutputStream());
        // 发送文件数据
        if (bfile != null && bfile.length>0) {
                StringBuilder sb1 = new StringBuilder();
                sb1.append(PREFIX);
                sb1.append(BOUNDARY);
                sb1.append(LINEND);
                sb1.append("Content-Disposition: form-data; name=\"file\"; filename=\""
                        + tempFileName + "\"" + LINEND);
                sb1.append("Content-Type: application/octet-stream; charset="
                        + CHARSET + LINEND);
                sb1.append(LINEND);
                outStream.write(sb1.toString().getBytes());
                outStream.write(bfile);
                outStream.write(LINEND.getBytes());
        }
        // 请求结束标志
        byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINEND).getBytes();
        outStream.write(end_data);
        outStream.flush();
        // 得到响应码
        int res = conn.getResponseCode();
        if (res == 200) {
            InputStream in = conn.getInputStream();
            InputStreamReader isReader = new InputStreamReader(in);
            BufferedReader bufReader = new BufferedReader(isReader);
            String line = "";
            String data = "";
            while ((line = bufReader.readLine()) != null) {
                data += line;
            }
            outStream.close();
            conn.disconnect();
            return data;
        }
        outStream.close();
        conn.disconnect();
        return null;
    }
}

 

base64文件上传

上一篇:v-html的问题及解决办法


下一篇:PHP 表单处理