相关文章推荐
要出家的海豚  ·  Excel VBA技巧 ...·  1 年前    · 
才高八斗的咖啡  ·  AdMob ...·  1 年前    · 
< groupId > com . amazonaws < / groupId > < artifactId > aws - java - sdk - s3 < / artifactId > < version > 1.11 .233 < / version > < / dependency > < dependency > < groupId > software . amazon . awssdk < / groupId > < artifactId > s3 < / artifactId > < version > 2.13 .63 < / version > < / dependency > < ! -- easyPoi -- > < dependency > < groupId > cn . afterturn < / groupId > < artifactId > easypoi - base < / artifactId > < version > 3.2 .0 < / version > < / dependency > < dependency > < groupId > cn . afterturn < / groupId > < artifactId > easypoi - web < / artifactId > < version > 3.2 .0 < / version > < / dependency > < dependency > < groupId > cn . afterturn < / groupId > < artifactId > easypoi - annotation < / artifactId > < version > 3.2 .0 < / version > < / dependency >

第一步,设置S3必要参数

   //accessKeyID
    public static String accessKeyID ="xxxx";
    //secretKey
    public static String secretKey = "xxxx";
    //S3上桶的名称
    public static String bucketName = "xxxx";
    //所属地区
    public static String region = "xxxx";
     * 创建访问凭证对象
    private static final BasicAWSCredentials awsCreds = new BasicAWSCredentials(accessKeyID, secretKey);
     * 创建s3对象
    private static final AmazonS3 s3 = AmazonS3ClientBuilder.standard()
            .withCredentials(new AWSStaticCredentialsProvider(awsCreds))
            //设置服务器所属地区
            .withRegion(Regions.CN_NORTHWEST_1)
            .build();

第三步,获取S3Object对象,filekey作为参数

public static S3Object getS3Object(String filekey){
    return s3.getObject(new GetObjectRequest(bucketName, filekey));

第四步,读取S3Object流并使用EasyPoi导出list集合

		String fileKey="20211402020231_辖区territory数据.xlsx";
        S3Object s3Object = S3utilsClass.getS3Object(fileKey);
        S3ObjectInputStream objectContent = s3Object.getObjectContent();
        //  转换成字符流
        //AreasTerritoryPoi为你的对象实体类
        List<AreasTerritoryPoi> poiList = ExcelUtils.importExcel(objectContent, 0, 1, true, AreasTerritoryPoi.class);
        System.out.println(poiList.toString());

附加:EasyPoi工具类

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
public class ExcelUtils {
	 * excel 导出
	 * @param list           数据
	 * @param title          标题
	 * @param sheetName      sheet名称
	 * @param pojoClass      pojo类型
	 * @param fileName       文件名称
	 * @param isCreateHeader 是否创建表头
	 * @param response
	public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName,
                                   boolean isCreateHeader, HttpServletResponse response) throws IOException {
		ExportParams exportParams = new ExportParams(title, sheetName, ExcelType.XSSF);
		exportParams.setCreateHeadRows(isCreateHeader);
		defaultExport(list, pojoClass, fileName, response, exportParams);
	 * excel 导出
	 * @param list      数据
	 * @param title     标题
	 * @param sheetName sheet名称
	 * @param pojoClass pojo类型
	 * @param fileName  文件名称
	 * @param response
	public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName,
			HttpServletResponse response) throws IOException {
		defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName, ExcelType.XSSF));
	 * excel 导出
	 * @param list         数据
	 * @param pojoClass    pojo类型
	 * @param fileName     文件名称
	 * @param response
	 * @param exportParams 导出参数
	public static void exportExcel(List<?> list, Class<?> pojoClass, String fileName, ExportParams exportParams,
			HttpServletResponse response) throws IOException {
		defaultExport(list, pojoClass, fileName, response, exportParams);
	 * excel 导出
	 * @param list     数据
	 * @param fileName 文件名称
	 * @param response
	public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response)
			throws IOException {
		defaultExport(list, fileName, response);
	 * 默认的 excel 导出
	 * @param list         数据
	 * @param pojoClass    pojo类型
	 * @param fileName     文件名称
	 * @param response
	 * @param exportParams 导出参数
	private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response,
			ExportParams exportParams) throws IOException {
		Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list);
		downLoadExcel(fileName, response, workbook);
	 * 默认的 excel 导出
	 * @param list     数据
	 * @param fileName 文件名称
	 * @param response
	private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response)
			throws IOException {
		Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);
		downLoadExcel(fileName, response, workbook);
	 * @param fileName 文件名称
	 * @param response
	 * @param workbook excel数据
	private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook)
			throws IOException {
		try {
			response.setCharacterEncoding("UTF-8");
			response.setHeader("content-Type", "application/vnd.ms-excel");
			response.setHeader("Content-Disposition", "attachment;filename="
					+ URLEncoder.encode(fileName + "." + ExcelTypeEnum.XLSX.getValue(), "UTF-8"));
			workbook.write(response.getOutputStream());
		} catch (Exception e) {
			throw new IOException(e.getMessage());
	 * excel 导入
	 * @param filePath   excel文件路径
	 * @param titleRows  标题行
	 * @param headerRows 表头行
	 * @param pojoClass  pojo类型
	 * @param <T>
	 * @return
	public static <T> List<T> importExcel(String filePath, Integer titleRows, Integer headerRows, Class<T> pojoClass)
			throws IOException {
		if (StringUtils.isBlank(filePath)) {
			return null;
		ImportParams params = new ImportParams();
		params.setTitleRows(titleRows);
		params.setHeadRows(headerRows);
		params.setNeedSave(true);
		params.setSaveUrl("/excel/");
		try {
			return ExcelImportUtil.importExcel(new File(filePath), pojoClass, params);
		} catch (NoSuchElementException e) {
			throw new IOException("模板不能为空");
		} catch (Exception e) {
			throw new IOException(e.getMessage());
	 * excel 导入
	 * @param file      excel文件
	 * @param pojoClass pojo类型
	 * @param <T>
	 * @return
	public static <T> List<T> importExcel(MultipartFile file, Class<T> pojoClass) throws IOException {
		return importExcel(file, 1, 1, pojoClass);
	 * excel 导入
	 * @param file       excel文件
	 * @param titleRows  标题行
	 * @param headerRows 表头行
	 * @param pojoClass  pojo类型
	 * @param <T>
	 * @return
	public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass)
			throws IOException {
		return importExcel(file, titleRows, headerRows, false, pojoClass);
	 * excel 导入
	 * @param file       上传的文件
	 * @param titleRows  标题行
	 * @param headerRows 表头行
	 * @param needVerfiy 是否检验excel内容
	 * @param pojoClass  pojo类型
	 * @param <T>
	 * @return
	public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, boolean needVerfiy,
                                          Class<T> pojoClass) throws IOException {
		if (file == null) {
			return null;
		try {
			return importExcel(file.getInputStream(), titleRows, headerRows, needVerfiy, pojoClass);
		} catch (Exception e) {
			throw new IOException(e.getMessage());
	 * excel 导入
	 * @param inputStream 文件输入流
	 * @param titleRows   标题行
	 * @param headerRows  表头行
	 * @param pojoClass   pojo类型
	 * @param <T>
	 * @return
	public static <T> List<T> importExcel(InputStream inputStream, Integer titleRows, Integer headerRows,
                                          boolean needVerify, Class<T> pojoClass) throws IOException {
		if (inputStream == null) {
			return null;
		ImportParams params = new ImportParams();
		params.setTitleRows(titleRows);
		params.setHeadRows(headerRows);
		params.setSaveUrl("/excel/");
		params.setNeedSave(true);
		params.setNeedVerfiy(needVerify);
		try {
			return ExcelImportUtil.importExcel(inputStream, pojoClass, params);
		} catch (NoSuchElementException e) {
			throw new IOException("excel文件不能为空");
		} catch (Exception e) {
			throw new IOException(e.getMessage());
	 * Excel 类型枚举
	enum ExcelTypeEnum {
		XLS("xls"), XLSX("xlsx");
		private String value;
		ExcelTypeEnum(String value) {
			this.value = value;
		public String getValue() {
			return value;
		public void setValue(String value) {
			this.value = value;

其他:S3工具类方法(包含上传下载)

//S3文件上传方法
    public static String S3upload(MultipartFile multipartFile){
        String localFileName = null;
        String s3FileFullPath = null;
        String originalFilename = multipartFile.getOriginalFilename();
        Date date = new Date();
        SimpleDateFormat formatter_yyyy = new SimpleDateFormat("yyyyHHmmhhmmss");
        if (multipartFile.isEmpty()) {
            return "上传文件不能为空";
        if(StringUtils.isEmpty(originalFilename)){
             localFileName =  "." + multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf(".") + 1)+"xlsx";
             s3FileFullPath = formatter_yyyy.format(date) + "_"+"校验结果报告"+ localFileName;
        }else{
            //生成随机文件名   localFileName
             localFileName =  "." + multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf(".") + 1);
            //在随机名前加上年月
             s3FileFullPath = formatter_yyyy.format(date) + "_"+originalFilename.substring(0,multipartFile.getOriginalFilename().lastIndexOf("."))+ localFileName;
        //获取S3Client对象
        AWSCredentials awsCredentials = new BasicAWSCredentials(accessKeyID, secretKey);
        AmazonS3ClientBuilder builder = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(awsCredentials));
        builder.setRegion(region);
        AmazonS3 s3Client = builder.build();
        ObjectMetadata metadata  = new ObjectMetadata();
        metadata.setContentType(multipartFile.getContentType());
        metadata.setContentLength(multipartFile.getSize());
        try {
            //开始上传文件
            //s3FileFullPath作为该文件的唯一标识符key
            PutObjectResult putResult = s3Client.putObject(bucketName, s3FileFullPath, multipartFile.getInputStream(), metadata);
            System.out.println("【流方式】上传MumbaiS3完成,md5:" + putResult.getETag() + ",S3文件:" + s3FileFullPath);
        } catch (Exception e) {
            e.printStackTrace();
        //返回文件在S3上的key
        return s3FileFullPath;

S3下载的方法(注意,此方法返回的是一个URL,需要触发才能下载文件)

//S3文件下载
    public static String downloadFile(String key){
        try {
            if(StringUtils.isEmpty(key)){
                return null;
            GeneratePresignedUrlRequest httpRequest = new GeneratePresignedUrlRequest(bucketName, key);
            return s3.generatePresignedUrl(httpRequest).toString();
        }catch (Exception e){
            e.printStackTrace();
        return null;
                    导入POM文件&lt;!--S3文件上传所需jar包--&gt;        &lt;dependency&gt;            &lt;groupId&gt;com.amazonaws&lt;/groupId&gt;            &lt;artifactId&gt;aws-java-sdk-s3&lt;/artifactId&gt;            &lt;version&gt;1.11.233&lt;/version&gt;        &lt;/dependen
为了配置您的本地环境,请使用下一个命令,并将xxxxxx替换为您的AWS凭证。
serverless config credentials --provider aws --key xxxxxxxx --secret xxxxxxxxxx
要检查您是否具有凭据,可以执行以下命令:
cat ~ /.aws/credentials
运行该命令:
sls deploy -v
                    CSDN-Ada助手: 
                    非常感谢博主的辛勤创作,这篇博客对于我来说非常有帮助。从标题就能看出博主的专业性和经验丰富,对于Docker Desk环境下的第三方软件安装问题进行了详细地讲解。文章中的步骤和操作都非常清晰明了,让我这个新手也能够跟着学习,真是受益匪浅。希望博主能够继续分享更多有价值的知识和经验,期待您的下一篇精彩文章。谢谢!
为了方便博主创作,提高生产力,CSDN上线了AI写作助手功能,就在创作编辑器右侧哦~(https://mp.csdn.net/edit?utm_source=blog_comment_recall )诚邀您来加入测评,到此(https://activity.csdn.net/creatActivity?id=10450&utm_source=blog_comment_recall)发布测评文章即可获得「话题勋章」,同时还有机会拿定制奖牌。
                easyExcel文件头部信息校验爬坑
                    qq502130297: 
                    我也碰到了,不知道为什么会被调用两次
                JAVA判断字符串以某个字母为开头
                    m0_67356969: 
                    public static void main(String[] args) {
        int prefix=0;
        int endfix=0;
        String string="peter piper picked a peck of pickled peppers";
        String[] arrStr = string.split(" ");
        for(int i=0;i<arrStr.length;i++){
            if(arrStr[i].startsWith("p")){prefix++;}
            if (arrStr[i].endsWith("r")) {endfix++;}
        System.out.println("以p为开头的字符串个数为"+prefix+"以r为结尾的字符串个数为"+endfix);
                MYSQL数据库
                    不敲代 码的程序员: 
                    牛逼666 大哥牛逼
                多线程处理一个大的LIST集合
                    qq_43025883: 
                    数据有时会顺序错乱   丢失的问题已解决   用你说的那个方法建list