com.kysoft.cpsi.audit.controller.SelfCheckAjaxUploadController.java Source code

Java tutorial

Introduction

Here is the source code for com.kysoft.cpsi.audit.controller.SelfCheckAjaxUploadController.java

Source

/**
 * Copyright (c) 2005-2012 https://github.com/zhangkaitao
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.kysoft.cpsi.audit.controller;

import java.io.*;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kysoft.cpsi.audit.service.SelfCheckService;

import net.sf.husky.attachment.utils.FileUploadUtils;
import net.sf.husky.exception.ExceptionUtils;
import net.sf.husky.log.MongoLogger;
import net.sf.husky.log.service.LogService;
import net.sf.husky.utils.HuskyConstants;

@Controller
public class SelfCheckAjaxUploadController {

    @Resource
    SelfCheckService selfCheckService;

    @Resource
    LogService logService;
    //? ??
    //    private long maxSize = FileUploadUtils.DEFAULT_MAX_SIZE;
    //?
    //    private String[] allowedExtension = FileUploadUtils.DEFAULT_ALLOWED_EXTENSION;

    /**
     * @param request
     * @param files
     * @return
     */
    @RequestMapping(value = "selfCheckUpload", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> ajaxUpload(HttpServletRequest request, HttpServletResponse response, String owner,
            String col, String ownerKey, String hcrwId, String hcclName, String hcsxmc, Integer nd) {

        Map<String, Object> result = new HashedMap();

        InputStream is = null;

        String filename = request.getHeader("X-File-Name");
        try {
            is = request.getInputStream();
            String mongoId = null;

            Map<String, Object> params = new HashedMap();
            params.put("dxnType", 2);
            Map<String, Object> dxnHccl2 = selfCheckService.getDXNHccl(params);
            params.put("dxnType", 1);
            Map<String, Object> dxnHccl1 = selfCheckService.getDXNHccl(params);
            if (hcclName.equals(dxnHccl2.get("NAME").toString())
                    && hcsxmc.equals(dxnHccl2.get("HCSXMC").toString())) {
                //?
                ByteArrayOutputStream baos = new ByteArrayOutputStream();

                byte[] buffer = new byte[1024];
                int len;
                while ((len = is.read(buffer)) > -1) {
                    baos.write(buffer, 0, len);
                }
                baos.flush();
                // ?
                InputStream is1 = new ByteArrayInputStream(baos.toByteArray());
                InputStream is2 = new ByteArrayInputStream(baos.toByteArray());
                //?
                selfCheckService.uploadSelfCheckData(is2, hcrwId, filename, nd);

                //?MONGODB
                mongoId = FileUploadUtils.mongoUpload(is1, filename, owner, ownerKey);
            } else if (hcclName.equals(dxnHccl1.get("NAME").toString())
                    && hcsxmc.equals(dxnHccl1.get("HCSXMC").toString())) {
                //???
                ByteArrayOutputStream baos = new ByteArrayOutputStream();

                byte[] buffer = new byte[1024];
                int len;
                while ((len = is.read(buffer)) > -1) {
                    baos.write(buffer, 0, len);
                }
                baos.flush();

                // ?
                InputStream is1 = new ByteArrayInputStream(baos.toByteArray());
                InputStream is2 = new ByteArrayInputStream(baos.toByteArray());

                //?
                if (filename.endsWith("xls") || filename.endsWith("xlsx")) {
                    selfCheckService.judgeRepeatExcle(is2, 5, 2, filename);
                }

                //?MONGODB
                mongoId = FileUploadUtils.mongoUpload(is1, filename, owner, ownerKey);
            } else {
                mongoId = FileUploadUtils.mongoUpload(is, filename, owner, ownerKey);
            }

            FileUploadUtils.updateOwner(owner, col, ownerKey, mongoId);

            response.setStatus(HttpServletResponse.SC_OK);
            result.put("status", 1);
            result.put("mongoId", mongoId);
            result.put("message", "??");

        } catch (Exception ex) {
            response.setStatus(HttpServletResponse.SC_OK);
            ex.printStackTrace();
            result.put("status", -1);
            result.put("mongoId", "");
            result.put("message", "??" + ex.getMessage());
            MongoLogger.warn("??", ExceptionUtils.getStackTrace(ex), hcrwId);
        } finally {
            try {
                is.close();
            } catch (IOException ignored) {
            }
        }
        return result;
    }

    @RequestMapping(value = "selfCheckUpload/dxnHccl", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getDXNHccl(String dxnType) {
        try {
            Map<String, Object> params = new HashedMap();
            params.put("dxnType", dxnType);
            Map<String, Object> dxnHccl = selfCheckService.getDXNHccl(params);
            Map<String, Object> result = new HashedMap();
            result.put("status", 1);
            result.put("message", "??");
            result.put("data", dxnHccl);
            return result;
        } catch (Exception e) {
            Map<String, Object> result = new HashedMap();
            result.put("status", -1);
            result.put("message", "?");
            result.put("data", "");
            return result;
        }
    }

    @RequestMapping(value = "selfCheckUpload/delete", method = RequestMethod.POST)
    @ResponseBody
    public String ajaxUploadDelete(HttpServletRequest request, @RequestParam(value = "filename") String filename)
            throws Exception {

        if (StringUtils.isEmpty(filename) || filename.contains("\\.\\.")) {
            return HuskyConstants.BLANK;
        }
        filename = URLDecoder.decode(filename, HuskyConstants.ENCODING_UTF8);

        String filePath = FileUploadUtils.extractUploadDir(request) + "/" + filename;

        File file = new File(filePath);
        file.deleteOnExit();

        return HuskyConstants.BLANK;
    }

    public enum State {
        OK(200, "?"), ERROR(500, ""), OVER_FILE_LIMIT(501,
                "??"), NO_SUPPORT_EXTENSION(502, "????");

        private int code;
        private String message;

        private State(int code, String message) {
            this.code = code;
            this.message = message;
        }

        public int getCode() {
            return code;
        }

        public String getMessage() {
            return message;
        }

    }

    /**
     * 
     * @param response
     * @param state
     */
    @SuppressWarnings("unused")
    private void responseMessage(HttpServletResponse response, State state) {
        response.setCharacterEncoding(HuskyConstants.ENCODING_UTF8);
        response.setContentType("text/html; charset=UTF-8");
        Writer writer = null;
        try {
            writer = response.getWriter();
            writer.write("{\"code\":" + state.getCode() + ",\"message\":\"" + state.getMessage() + "\"}");
            writer.flush();
            writer.close();
        } catch (Exception e) {
        } finally {
            IOUtils.closeQuietly(writer);
        }
    }
}