java后台文件处理相关问题

(一)文件后缀问题:

获取文件名:

String name = multipartFile.getOriginalFilename();

获取后缀,文件类型,小写:

String postFix = FileUtil.extName(name).toLowerCase();

判断是否为图片格式:

public static boolean isImage(String extName) {
		String imgs[] = { "bmp", "jpg", "jpeg", "png", "tiff", "gif", "pcx", "tga", "exif", "fpx", "svg", "psd",
				"cdr", "pcd", "dxf", "ufo", "eps", "ai", "raw", "wmf" };
		return ArrayUtil.containsIgnoreCase(imgs, extName);
	}

其他格式 可以类推。

(二)图片获取传入前台:

这里我传入的是文件流:

  @RequestMapping(method = RequestMethod.GET, value = "/getStorageImage")
    public void getStorageImage(HttpServletRequest request, HttpServletResponse response, String id) throws IOException {

        ValidateUtil.isNotBlank(id, "文件编号非法,找不到对应文件");
        OutputStream stream = null;
        TaskPhotoSd taskPhotoSd = taskPhotoSdService.selectById(id);
        try {
            if (taskPhotoSd != null) {
                FastDfsFile fastDfsFile = new FastDfsFile();
                String fileType = StrUtil.subAfter(taskPhotoSd.getStoragePath(), ".", false);
                fastDfsFile.setRemoteFileUrl(StrUtil.subAfter(taskPhotoSd.getStoragePath(), "/", false));
                fastDfsFile.setGroupName(StrUtil.subBefore(taskPhotoSd.getStoragePath(), "/", false));
                byte[] img = fastDfsUtil.downloadFile(fastDfsFile);
                response.addHeader("Content-Disposition", "attachment;filename=" + new String((taskPhotoSd.getPhotoNmae() + "." + fileType).getBytes("GBK"), "ISO8859_1"));
                response.addHeader("Content-Length", "" + img.length);
                response.setContentType("image/jpg");
                stream = new BufferedOutputStream(response.getOutputStream());
                stream.write(img);
                stream.flush();
            } else {
                throw new ValidateException("未找到");
            }
        } catch (Exception e) {
            throw e;
        } finally {
            FortifyUtil.close(stream);
        }
    }

这里我使用的是fastdfs工具类下载的文件,byte数组获取内容,根据url路径,可以按照不同的方法来获取。

其中上面的代码是浏览器会自动下载,如果要进行预览操作,可以将response.addHeader方法进行注释。

(三)excel文件导出:

	@SystemControllerLog(desc="导出")
	@RequestMapping(method=RequestMethod.GET, value = "exportProject")
    public void exportProject(Project project, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
            String fileName = "项目数据"+DateUtil.now()+".xlsx";
            List<ProjectExcel> projects = projectService.exportGetData();
    		new ExcelExport("项目数据", ProjectExcel.class).setDataList(projects).write(response, fileName).dispose();
		} catch (Exception e) {
			renderResult(response, RestResultGenerator.success("导出项目失败!失败信息:"+e.getMessage()));
		}
    }

ExcelExport文件如下:

package com.glens.common.excel;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Comment;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;

import com.glens.common.excel.ExcelField.Align;
import com.glens.common.excel.ExcelField.Type;
import com.glens.common.exception.ExcelException;
import com.glens.common.util.FortifyUtil;

/**
 * 导出Excel文件(导出“XLSX”格式,支持大数据量导出 )
*
* @ClassName: ExcelExport
* @Description: TODO(这里用一句话描述这个类的作用)
* @author XuBing
* @date 2018年3月13日 下午4:37:01
 */
public class ExcelEx
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值