前台:layui 后台:springboot(MultipartFile)实现多文件上传

ftl文件

文件名 大小 状态 路径 操作

jquery代码

 

java后台代码


package com.w3china.mingjing3.web.controller;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

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

import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.w3china.mingjing3.web.FileStorage;
import com.w3china.mingjing3.web.GlobalConfig;
import com.w3china.mingjing3.web.service.SiteService;
import com.w3china.mingjing3.model.SiteConfig;
import com.w3china.mingjing3.model.SiteSettings;

import org.springframework.web.multipart.MultipartHttpServletRequest;

@Controller
@RequestMapping("/file")
public class FileController {
	
	
	
	@Autowired
	private GlobalConfig globalConfig;

	@Autowired
	private SiteService siteService;
	
	@Autowired
	public FileStorage fileStorage;
	
	


	@RequestMapping(value="uploads")
	@ResponseBody
	public String uploads(
			@RequestParam("file")MultipartFile sortPicImg,
			@RequestParam("cate")String cate,
			HttpServletRequest request,
			HttpServletResponse response) {

		try {
			String fname = sortPicImg.getOriginalFilename().toLowerCase();

			String suffix = "";
			if (fname.lastIndexOf(".") > 1) {
				suffix = fname.substring(fname.lastIndexOf(".") + 1);
			}
			if ("doc".equals( suffix ) ||
					"docx".equals( suffix ) ||
					"pdf".equals( suffix ) ||
					"xls".equals( suffix ) ||
					"xlsx".equals( suffix ) ||
					"ppt".equals( suffix ) ||
					"pptx".equals( suffix )) {

				String fileName = System.currentTimeMillis()+"_"+fname;

				String objectName = cate + "/" + fileName;

				//获取流长度
				long contentLength = sortPicImg.getSize();

				SiteSettings settings = getSiteSettings(request);

				fileStorage.save( objectName, contentLength, sortPicImg.getInputStream(), settings );

				String fileUrl = "http://" + settings.getOssOuterDomain() + "/" + cate + "/" + fileName;
				String resp = "{\"code\":0, \"msg\":\"success\", \"fileUrl\":\"" + fileUrl + "\", \"fileName\":\"" + sortPicImg.getOriginalFilename() + "\", \"fileSize\":" + sortPicImg.getSize() + " }";
				System.out.println(resp);
				return resp;
			} else {

				return "{\"code\":0,\"msg\":\"文件格式不支持。\"}";
			}
		} catch (IOException e) {
			System.out.println(e.toString());

			return "{\"code\":0,\"msg\":\"error\"}";
		}
	}
}

 

你可能感兴趣的:(java,文件,layui)