当前位置 : 主页 > 网络编程 > JavaScript >

jquery上传组件应用

来源:互联网 收集:自由互联 发布时间:2021-06-28
FileUploadController.java package com.lincomb.scene.input.controller;import com.lincomb.base.util.Base64;import com.lincomb.base.util.FastdfsUtil;import com.lincomb.scene.input.common.constant.Constant;import com.lincomb.scene.input.common.
FileUploadController.java
package com.lincomb.scene.input.controller;

import com.lincomb.base.util.Base64;
import com.lincomb.base.util.FastdfsUtil;
import com.lincomb.scene.input.common.constant.Constant;
import com.lincomb.scene.input.common.controller.BaseController;
import com.lincomb.scene.input.common.util.Assert;
import com.lincomb.scene.input.enums.RespCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
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.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @Description:
 * @Author: caijun.wei
 * @CreateDate:2017-10-21 11:43
 * Version: V0.1
 */
@Slf4j
@Controller
@RequestMapping
public class FileUploadController extends BaseController {
    
    @RequestMapping("fileUpload")
    public ModelAndView fileUpload(String file) {
    	
    	Assert.isBlank(file, RespCode.REQUEST_PARAM_NULL);
    	file = file.substring(file.indexOf(",") + 1);
        ModelMap modelMap = new ModelMap();
        try {
            byte[] fContent = Base64.decode(file.getBytes());
            String path = FastdfsUtil.upload(fContent, "1.jpg", fContent.length);
            modelMap.addAttribute("path", path);
            modelMap.addAttribute(Constant.ResponseVO.CODE, RespCode.RESP_CODE_SUCCESS.getCode());
            modelMap.addAttribute(Constant.ResponseVO.MSG, RespCode.RESP_CODE_SUCCESS.getMsg());
        }catch (Exception e) {
            log.error("上传文件失败:" + e.getMessage(), e);
            modelMap.addAttribute(Constant.ResponseVO.CODE, RespCode.RESP_CODE_FAILER.getCode());
            modelMap.addAttribute(Constant.ResponseVO.MSG, RespCode.RESP_CODE_FAILER.getMsg());
        }
        return new ModelAndView("jsonView", modelMap);
    }

    /**
     * 图片上传
     * @param files 图片name
     * @param request 
 
* @return 图片可访问地址,没有前缀域名http */ @RequestMapping(value = "/pic",method = RequestMethod.POST) public ModelAndView uploadPic(@RequestParam(value = "files",required = false) MultipartFile[] files, HttpServletRequest request) { String dfsDomain = "http://172.16.103.204"; ModelMap model = new ModelMap(); List urlList = new ArrayList<>(); List imgList=new ArrayList<>(); for (MultipartFile file : files) { if (file.isEmpty()) { model.addAttribute(Constant.ResponseVO.CODE, RespCode.REQUEST_PARAM_NULL.getCode()); model.addAttribute(Constant.ResponseVO.MSG, RespCode.REQUEST_PARAM_NULL.getMsg()); } else { //生成uuid作为文件名称 String uuid = UUID.randomUUID().toString().replaceAll("-",""); String contentType=file.getContentType(); //获得文件后缀名称 String imageName=contentType.substring(contentType.indexOf("/")+1); String fileName=uuid+"."+imageName; //借助dfs上传文件 try { byte[] bytes = file.getBytes(); String url = FastdfsUtil.upload(bytes, fileName, file.getSize()); urlList.add(dfsDomain + url); imgList.add(fileName); } catch (IOException e) { log.info(String.format("文件上传失败:%s",e)); model.addAttribute(Constant.ResponseVO.CODE, RespCode.RESP_CODE_FAILER.getCode()); model.addAttribute(Constant.ResponseVO.MSG, RespCode.RESP_CODE_FAILER.getMsg()); } } } model.addAttribute("urlList", urlList); model.addAttribute("imgList", imgList); model.addAttribute(Constant.ResponseVO.CODE, RespCode.RESP_CODE_SUCCESS.getCode()); model.addAttribute(Constant.ResponseVO.MSG, RespCode.RESP_CODE_SUCCESS.getMsg()); return new ModelAndView("jsonView", model); } }
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>



    
 
    




 













 
    
  
    
   multiple>
    
  
    
   
    
  
    
  
    
  
    

 





 







 



 



 







上一篇:node.js之Socket.IO Helper
下一篇:rem.js
网友评论