工具类中往静态方法中注入Bean
@Component
public class QrCodeUtils {
private static TestService testService;
@Autowired
public QrCodeUtils(TestServicetestService) {
QrCodeUtils.testService= testService;
}
/**
* 上传文件
*
* @param string 字符串
* @return {@link String}
*/
public static String createQrCodeByString(String string) {
fileAddress="C:/a.jpg"
try {
// 创建file对象
File file = new File(fileAddress);
// 获取file对象的文件输入流
FileInputStream input = new FileInputStream(file);
MultipartFile multipartFile =
new MockMultipartFile(name, file.getName(), "image/jpg", IOUtils.toByteArray(input));
fileName = testService.uploadFile(multipartFile);
input.close();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
FileUploadUtils.deleteFile(fileAddress);
}
String objectUrl = minioUtils.getObjectURL(fileName);
System.out.println(objectUrl);
return objectUrl ;
}
}
注意: 工具类需要添加@Component 注解,此前因忘记添加多次失败