Spring 单图上传 返回图片访问路径

pom文件引入

   
        
            commons-fileupload
            commons-fileupload
            1.2.2
        


        
            commons-io
            commons-io
            2.0.1
        
package com.xyy.controller;
import com.xyy.commons.ServerResponse;
import com.xyy.server.FileService;
import com.xyy.util.MyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import static com.xyy.server.impl.FileServiceImpl.NextInt;


/**
 * 基础设置控制
 *
 * @author Administrator
 */
@RequestMapping("api/file")
@RestController
public class FileUploadController {

    @Autowired
    private FileService fileService;

    /**
     *  上传图片 参数fileUpload  返回图片访问路径
     * @param request
     * @return
     */
    @PostMapping("fileUpload")
    public ServerResponse fileUpload2(HttpServletRequest request) {
        // 设置上下方文
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
                request.getSession().getServletContext());
        //文件名
        String fileName = "";
        //获取tomcat项目路径
        String url = MyUtil.getTomcatUrl();

        // 检查form是否有enctype="multipart/form-data"
        if (multipartResolver.isMultipart(request)) {
            MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
            Iterator iter = multiRequest.getFileNames();
             while (iter.hasNext()) {
                 // 由CommonsMultipartFile继承而来,拥有上面的方法.
                 MultipartFile file = multiRequest.getFile(iter.next());
                 if (file != null) {
                     //原文件名
                     String type = file.getOriginalFilename();
                     //获取文件类型
                     type = type.substring(type.lastIndexOf("."), type.length());
                     //重命名
                     fileName = NextInt(10000, 99999) + System.currentTimeMillis() + type;
                     //path 图片保存路径  保存到war包根目录    request.getContextPath()  war包名
                     String path = (new File(request.getServletContext().getRealPath("/")))
                             .getParent() + "/upload" + request.getContextPath() + "/image";

                     File localFile = new File(path);
                     // 如果文件夹不存在  创建文件夹
                     if (!localFile.exists()) {
                         localFile.mkdirs();
                     }

                     File targetFile = new File(path, fileName);
                     try {
                         //保存图片
                         file.transferTo(targetFile);
                     } catch (IOException e) {
                         e.printStackTrace();
                         return ServerResponse.createByErrorMessage("");
                     }
                 }
             }
        }
        //url 图片路径
        url = url +"upload" + request.getContextPath() + "/image/"+ fileName;
        return ServerResponse.createBySuccessMessage(url);
    }
}

你可能感兴趣的:(Spring 单图上传 返回图片访问路径)