servlet 获取post的zip格式参数

本文介绍了一种在服务端接收并处理通过HTTP POST方式发送的ZIP压缩数据的方法。主要步骤包括:获取数据流,将其写入临时文件,不解压直接读取数据,最后将处理后的数据写入log4j日志。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

服务端获取通过http post方式传的经过zip压缩后的数据,需求是对数据处理后,写入log4j.
步骤:
1、获取数据流;
2、数据写入中间文件;
3、不解压生成文件,直接读取数据;
4、对数据处理后写入log4j。

//controller类
import com.baomihua.utils.Commons;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

@Controller
public class apptest {
    @RequestMapping(value="/t",method= RequestMethod.POST)
    public void app(HttpServletRequest req, HttpServletResponse rep) throws Exception{
    	//临时文件在服务器目录(appdata为文件名)
        String file = "/usr/local/tomcat/apache-tomcat-7.0.93/appdata";
        //获取数据流
        ServletInputStream inputStream = req.getInputStream();
        //调用方法,将数据流写入临时文件
        Commons.writeToLocal(file,inputStream);
        //对临时文件数据解压
        ZipFile zf = new ZipFile(file);
        InputStream in = new BufferedInputStream(new FileInputStream(file));
        ZipInputStream zin = new ZipInputStream(in);
        ZipEntry ze;
        while ((ze = zin.getNextEntry()) != null) {
            if (ze.isDirectory()) {
            } else {
                long size = ze.getSize();
                if (size > 0) {
                    BufferedReader br = new BufferedReader(
                            new InputStreamReader(zf.getInputStream(ze)));
                    String line;
                    while ((line = br.readLine()) != null) {
                        //调用方法对解压后的数据处理,并写入log4j
                        writeData.appData(req,rep,line);
                    }
                    br.close();
                }
            }
        }
        zin.closeEntry();

    }
}

写入本地文件方法

/**
     * 将InputStream写入本地文件
     * @param destination 写入本地目录
     * @param input 输入流
     * @throws IOException IOException
     */
    public static void writeToLocal(String destination, InputStream input)
            throws IOException {
        int index;
        byte[] bytes = new byte[1024];
        FileOutputStream downloadFile = new FileOutputStream(destination);
        while ((index = input.read(bytes)) != -1) {
            downloadFile.write(bytes, 0, index);
            downloadFile.flush();
        }
        input.close();
        downloadFile.close();

    }

简单处理数据后,将数据写入log4j

public class writeData {
    private static final Log logger = LogFactory.getLog("logger");
    public static void appData(HttpServletRequest req, HttpServletResponse rep,String str)throws Exception{
         List<String> logList = new ArrayList<String>();
           ...(数据处理过程)
            StringBuffer line = new StringBuffer();
            Boolean isfirst = Boolean.valueOf(true);
            Iterator<String> it = logList.iterator();
            while (it.hasNext()) {
                if (isfirst.booleanValue())
                    isfirst = Boolean.valueOf(false);
                else
                    line.append("\001");
                line.append((String) it.next());
            }
            logger.info(line.toString().replace("\n", "").replace("\r", ""));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值