h5单文件上传

前台h5

Html5单文件上传详解

Html5单文件上传详解

Html5单文件上传详解

后台action

  1. 后台使用SpringMVC做文件上传,其底层也是依赖Apache的fileUplaod库
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.lct.domain.Client;
import com.lct.service.ClientService;
import com.lct.service.ResourceService;
import com.lct.utils.FileWmxUtils;
import com.lct.utils.UDPUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018/3/8 0008.
 * 资源控制器
 */
@Controller
@RequestMapping("resourceController")
public class ResourceController {
    @Resource
    private ResourceService resourceService;
    @Resource
    private ClientService clientService;
/**
 * 单文件上传
 *
 * @param uploadFile:上传的文件封装好的对象
 * @param info:关于文件的描述
 * @param request
 * @return ----逻辑--流程----
 * 1、当上传的文件在服务器指定的目录下已经存在时
 * 1)比对两者文件大小,如果一致,则放弃本次上传
 * 2)比对两者文件大小,如果不一致,则根据规则按顺序递增命名文件,如xxx(2)xxx(3)等,
 * 1>上传文件,然后新增数据库
 * 2、当上传的文件在服务器指定的目录下不存在时
 * 1)上传文件,然后新增数据库
 */
@RequestMapping("singleFileUpload.action")
public String singleFileUpload(@RequestParam(required = true) MultipartFile uploadFile, String info, HttpServletRequest request) {
    try {
        /** 获取服务器真实路径*/
        ServletContext servletContext = request.getServletContext();
        String realPath = servletContext.getRealPath("/uploadFile");

        info = StringUtils.isBlank(info) ? null : info.trim();

        /**获取上传文件名以及大小
         *fileNameAndFormat:包含文件名与格式,如:123.mp4
         *fileName:文件名,如:123
         *fileFormat:文件格式,如:.mp4
         *fileSizeLong:文件大小,long型,单位字节,如:1024
         *fileSizeStr:文件大小,字符型,如:10M
         * fileType:文件类型
         * */
        String fileNameAndFormat = uploadFile.getOriginalFilename();
        String fileName = fileNameAndFormat.substring(0, fileNameAndFormat.lastIndexOf("."));
        String fileFormat = fileNameAndFormat.substring(fileNameAndFormat.lastIndexOf("."));
        Long fileSizeLong = uploadFile.getSize();
        String fileSizeStr = FileUtils.byteCountToDisplaySize(fileSizeLong);
        Integer fileType = FileWmxUtils.getFileTypeByFileFormat(fileFormat);

        File saveFile = new File(realPath + File.separator + fileNameAndFormat);
        if (saveFile.exists()) {
            /**1、当上传文件在服务器中已经存在时*/
            if (saveFile.length() == fileSizeLong) {
                System.out.println("文件服务器中已经存在,且大小一致,放弃本次上传...");
            } else {
                System.out.println("文件服务器中已经存在,但大小不一致...");
                int count = 2;
                while (count > 0) {
                    System.out.println("---------------:" + count);
                    String fileNameNew = fileName + "(" + count + ")";
                    String saveFileNewPath = realPath + "/" + fileNameNew + fileFormat;
                    File saveFileNew = new File(saveFileNewPath);
                    if (!saveFileNew.exists()) {
                        saveFileNew.createNewFile();
                        uploadFile.transferTo(saveFileNew);
                        resourceService.insertResource(fileNameNew + fileFormat, fileSizeStr, fileType, "uploadFile/" + fileFormat, new Date(), null, info);
                        break;
                    }
                    count++;
                }
            }
        } else {
            /**2、当上传文件在服务器中不存在时
             * transferTo方法必须要求文件已经存在,所以必须新建*/
            if (!saveFile.getParentFile().exists()) {
                saveFile.getParentFile().mkdirs();
            }
            saveFile.createNewFile();
            uploadFile.transferTo(saveFile);
            resourceService.insertResource(fileName + fileFormat, fileSizeStr, fileType, "uploadFile/" + fileNameAndFormat, new Date(), null, info);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return "redirect:/resourceController/resourceCenterList.action?activeMenu=3";
}


相关文章: