import com.alibaba.gts.flm.common.utils.Base64Util;
import com.alibaba.gts.flm.dd.api.web.controller.model.UrlReq;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
@RestController
@RequestMapping("/file")
public class FileController {
@Value("${local.ip:-}")
private String localIp;
@RequestMapping("/getByUrl")
public void getByUrl(String urls, HttpServletResponse response) throws Exception {
URL url = new URL(urls);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
InputStream inputStream = connection.getInputStream();
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = bufferedInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
byte[] imageBytes = outputStream.toByteArray();
response.setContentType("image/jpeg"); // 根据实际需要设置响应的内容类型
response.getOutputStream().write(imageBytes);
bufferedInputStream.close();
outputStream.close();
}
@RequestMapping("/getByUrl2")
public void getByUrl2(String urls, HttpServletResponse response) throws Exception {
String str = Base64Util.base64Restore(urls);
if (!str.contains("http")){
str = "http://"+localIp+":32033"+str;
}
URL url = new URL(str);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
InputStream inputStream = connection.getInputStream();
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = bufferedInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
byte[] imageBytes = outputStream.toByteArray();
response.setContentType("image/jpeg"); // 根据实际需要设置响应的内容类型
response.getOutputStream().write(imageBytes);
bufferedInputStream.close();
outputStream.close();
}
@RequestMapping("/getByUrl3")
public void getByUrl3(@RequestBody UrlReq req, HttpServletResponse response) throws Exception {
URL url = new URL(req.getUrl());
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
InputStream inputStream = connection.getInputStream();
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = bufferedInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
byte[] imageBytes = outputStream.toByteArray();
response.setContentType("image/jpeg"); // 根据实际需要设置响应的内容类型
response.getOutputStream().write(imageBytes);
bufferedInputStream.close();
outputStream.close();
}
}
Base64Util
import java.util.Base64;
public class Base64Util {
public static String base64Restore(String str) {
byte[] decodedBytes = Base64.getDecoder().decode(str);
return new String(decodedBytes);
}
public static String str2Base64(String str) {
return Base64.getEncoder().encodeToString(str.getBytes());
}
}