julie.com.mikaelson.file.controller.FileInfoController.java Source code

Java tutorial

Introduction

Here is the source code for julie.com.mikaelson.file.controller.FileInfoController.java

Source

/******************************************************************************
 * @File name : FileInfoController.java
 *
 * @Author : Administrator
 *
 * @Date : 2016510
 *
 * @Copyright Notice:
 * Copyright (c) 2020 Niklaus Mikaelson, Inc. All Rights Reserved.
 * This software is published under the terms of the Niklaus Mikaelson Software
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 *
 *
 *
  ----------------------------------------------------------------------------
 * Date             Who       Version    Comments
 * 2016510?4:18:41    Administrator    1.0       Initial Version
 **************************************************************************************/

package julie.com.mikaelson.file.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

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

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 org.springframework.web.multipart.MultipartFile;

/**
 *
 */
@Controller
@RequestMapping("/file")
public class FileInfoController {

    @RequestMapping("/upload")
    public String provideUploadInfo() {
        return "upload";
    }

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public void uploadFile(@RequestParam("fileUpload") MultipartFile file) {

        System.out.println("+++++++++++/uploadfile invoke");
    }

    public @ResponseBody String handleFileUpload(@RequestParam("name") String name,
            @RequestParam("file") MultipartFile file) {
        if (!file.isEmpty()) {
            try {
                byte[] bytes = file.getBytes();
                BufferedOutputStream stream = new BufferedOutputStream(
                        new FileOutputStream(new File(name + "-uploaded")));
                stream.write(bytes);
                stream.close();
                return "You successfully uploaded " + name + " into " + name + "-uploaded !";
            } catch (Exception e) {
                return "You failed to upload " + name + " => " + e.getMessage();
            }
        } else {
            return "You failed to upload " + name + " because the file was empty.";
        }
    }

    /*
       @RequestMapping("/uploadfile")
       public String uploadFile(File image_file) {
        
          System.out.println("+++++++++++/uploadfile invoke");
        
          return "home";
        
       }*/

    @RequestMapping(value = "/uploadfile", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> handleRequest(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String fileName = request.getParameter("image_file");
        fileName = new String(fileName.getBytes("ISO-8859-1"), "UTF-8");
        String overFlg = request.getParameter("over"); // 0:go on;1:over
        System.out.println("get: " + fileName);
        byte[] buf = new byte[1024];

        File file = new File("./" + /*
                                    * "[" +
                                    * UUID.randomUUID().toString() +
                                    * "]" +
                                    */fileName);
        InputStream is = null;
        BufferedOutputStream fileOut = new BufferedOutputStream(new FileOutputStream(file, true));
        try {

            is = request.getInputStream();

            while (true) {

                int bytesIn = is.read(buf, 0, 1024);
                System.out.println(bytesIn);
                if (bytesIn == -1) {
                    break;
                } else {
                    fileOut.write(buf, 0, bytesIn);
                }
            }

            fileOut.flush();
            fileOut.close();
            System.out.println(file.getAbsolutePath());
        } catch (IOException e) {
            System.out.println("error  info");
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("over", overFlg);
        map.put("data", fileName);
        return map;
    }

}