com.suntek.gztpb.controller.DriverLicenseController.java Source code

Java tutorial

Introduction

Here is the source code for com.suntek.gztpb.controller.DriverLicenseController.java

Source

package com.suntek.gztpb.controller;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
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.ResponseBody;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.suntek.gztpb.core.BaseContoller;
import com.suntek.gztpb.core.ServiceException;
import com.suntek.gztpb.model.DriverLicenseModel;
import com.suntek.gztpb.service.DriverLicenseService;
import com.suntek.gztpb.util.CommonUtil;
import com.suntek.gztpb.util.IdGenerator;

@Controller
@RequestMapping(value = "driverlicense")
public class DriverLicenseController extends BaseContoller implements ServletContextAware {

    @Autowired
    private DriverLicenseService driverLicenseService;

    private static Logger logger = Logger.getLogger(VehicleLicenseController.class);

    private ServletContext servletContext;

    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;

    }

    //http://localhost:8080/gztpbwebsite/driver/apply.htm
    @RequestMapping(value = "apply.htm", method = RequestMethod.GET)
    public String apply() {
        return "/DriverLicenseApply";
    }

    @RequestMapping(value = "saveApply.htm", method = RequestMethod.POST)
    public String saveApply(DriverLicenseModel apply, HttpServletRequest request, HttpServletResponse response)
            throws ServiceException, IOException {
        response.setContentType("text/html");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        try {
            String applyNum = IdGenerator.getInstance().getBizCode("ITMS_SEQ", 5);
            apply.setApplyNum(applyNum);
            driverLicenseService.saveApply(apply);
            out.write("<script>parent.saveCallback(1,'" + applyNum + "')</script>");
        } catch (Exception e) {
            logger.error(e.getMessage());
            out.write("<script>parent.saveCallback(0)</script>");
        }

        return null;
    }

    @RequestMapping(value = "validateCode.htm", method = RequestMethod.POST)
    public @ResponseBody String validateCode(HttpServletRequest request) throws Exception {
        String certCode = request.getSession().getAttribute("certCode").toString();
        String code = request.getParameter("code");
        if (code.equalsIgnoreCase(certCode)) {
            return "true";
        }
        return "false";
    }

    @RequestMapping(value = "upload.htm", method = RequestMethod.POST) //
    public String handleFormUpload(HttpServletRequest request, HttpServletResponse response) throws IOException {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        String inputName = request.getParameter("name");
        CommonsMultipartFile mFile = (CommonsMultipartFile) multipartRequest.getFile(inputName);

        if (!mFile.isEmpty()) {
            String path = this.servletContext.getRealPath("/picUpload/");
            SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
            String fileName = format.format(new Date()) + "_" + mFile.getOriginalFilename();
            File fold = new File(path);
            if (!fold.exists()) {
                fold.mkdir();
            }
            path = path + "\\" + fileName; // ?

            File file = new File(path); // 

            response.setContentType("text/html");
            response.setCharacterEncoding("utf-8");
            PrintWriter out = response.getWriter();

            try {
                mFile.getFileItem().write(file); // 
                out.write("<script>parent.callback(1,'" + fileName + "','" + inputName + "')</script>");
            } catch (Exception e) {
                logger.error(e.getMessage());
                out.write("<script>parent.callback(0)</script>");
            }
        }
        return null;
    }

}