java调用接口实现文件上传

java

import com.gap.open.core.base.IOpenApi;

import com.gap.open.core.base.OpenAuthConfig;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.collections.map.HashedMap;

import org.springframework.context.annotation.Configuration;

import org.springframework.web.multipart.MultipartFile;

import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;

import java.io.*;

import java.net.HttpURLConnection;

import java.net.URL;

import java.util.Map;

import java.util.UUID;

/**

* @author huqi 2021/8/12

*/

@Slf4j

@Configuration

public class UploadFile {

@Resource

private OpenAuthConfig openAuthConfig;

public File transferToFile(MultipartFile multipartFile) {

// 选择用缓冲区来实现这个转换即使用java 创建的临时文件 使用 MultipartFile.transferto()方法 。

File file = null;

try {

String filename = multipartFile.getResource().getFilename();

String suffix = filename.substring(filename.lastIndexOf("."));

file=File.createTempFile(UUID.randomUUID().toString(), suffix);

multipartFile.transferTo(file);

file.deleteOnExit();

} catch (IOException e) {

e.printStackTrace();

}

return file;

}

public String upLoadFilePost(String actionUrl, MultipartHttpServletRequest request, String token) throws IOException {

Map<String, MultipartFile> fileTemp = request.getFileMap();

Map<String, File> files = new HashedMap();

for(String key : fileTemp.keySet()){

files.put(key,transferToFile(fileTemp.get(key)));

}

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 (files != null) {

for (Map.Entry<String, File> file : files.entrySet()) {

StringBuilder sb1 = new StringBuilder();

sb1.append(PREFIX);

sb1.append(BOUNDARY);

sb1.append(LINEND);

sb1.append("Content-Disposition: form-data; name=\"file\"; filename=\""

+ file.getValue().getName() + "\"" + LINEND);

sb1.append("Content-Type: application/octet-stream; charset="

+ CHARSET + LINEND);

sb1.append(LINEND);

outStream.write(sb1.toString().getBytes());

InputStream is = new FileInputStream(file.getValue());

byte[] buffer = new byte[1024];

int len = 0;

while ((len = is.read(buffer)) != -1) {

outStream.write(buffer, 0, len);

}

is.close();

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;

}

}

 

以上是 java调用接口实现文件上传 的全部内容, 来源链接: utcz.com/z/389677.html

回到顶部