上传文件

package com.csf.upload.service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

import javax.activation.DataHandler;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.jaxrs.ext.multipart.Attachment;
import org.apache.cxf.jaxrs.ext.multipart.Multipart;
import org.apache.log4j.Logger;
import org.apache.poi.util.IOUtils;

import com.csf.upload.base.BaseServiceImpl;
import com.csf.upload.utils.PropertyConfigurer;

/**
 * 
 * @author fenglei.ma
 *
 */
@Path("/csv")
@Produces("application/json")
public class CsvService extends BaseServiceImpl {
    Logger log = Logger.getLogger(getClass());

    /**
     * 上传 分析
     * file 文件名
     */
    @POST
    @Path("/upload")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public String upload(@Context HttpServletRequest request, @Multipart(value = "csvfile") Attachment csvfile,
            @Multipart("file") String file) {
        try {
            DataHandler dataHandler = csvfile.getDataHandler();
            String name = dataHandler.getName(); // 因为中文乱码问题,下面采用覆盖
            log.info("-------->上传文件的名称 :" + name);
            name = file;
            log.info("-------->file:" + file);

            if (StringUtils.isNotBlank(name)) {
                InputStream is = dataHandler.getInputStream();
                byte[] bytes = IOUtils.toByteArray(is);

                String contexPath = PropertyConfigurer.getContextProperty("upload.address.path");
                log.info("data path:" + contexPath);
                String fileName = contexPath + LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"))
                        + File.separator + name;
                log.info(fileName);
                writeFile(bytes, fileName);
            }

            return this.returnSuccAsJson("success");
        } catch (Exception e) {
            log.error(e, e);
            return this.returnExceptionAsJson(e);
        }
    }

    private void writeFile(byte[] content, String filename) throws IOException {
        File file = new File(filename);
        // 目标文件所在的目录不存在则创建
        if (!file.getParentFile().exists()) {
            if (file.getParentFile().mkdirs()) {
                log.info("【 " + file.getParentFile() + " 】 目录不存在,已经创建");
            }
        }
        if (!file.exists()) {
            file.createNewFile();
        }
        FileOutputStream fop = new FileOutputStream(file);
        fop.write(content);
        fop.flush();
        fop.close();
    }

}

/*<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-rs-extension-providers</artifactId>
    <version>3.1.0</version>
</dependency>

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.17</version>
</dependency>*/

 

转载于:https://www.cnblogs.com/xiaolei2017/p/8269242.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值