配置tomcat本地服务器并且上传图片

我想搭建一个本地的tomcat服务器然后想将图片保存在服务器里,通过locahost:8080这样的路径访问,有大神指点一下吗,能详细点最好。

下载Tomcat安装版,安装Tomcat。
将所要展示的图片放置到安装目录中webapps/ROOT/目录下,启动Tomcat,可以使用localhost:8080/图片名称 即可访问。

可以在WEB-INF里指定静态地址
也可以使用nginx静态代理

IIS服务器安装ftp服务器,很方便上传和下载图片~~~~

你可以本地安装一个nginx,将图片放到nginx的html文件夹中,启动nginx,你就可以通过localhost:80/图片名称来访问了

package com.oak.myoffices.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;

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.commons.CommonsMultipartFile;

/**

  • 上传单个文件
  • @author Administrator
    *
    /
    @Controller

    public class UploadController {
    @RequestMapping("/upload")
    @ResponseBody
    /
    *

    • 前台上传的文件,后台默认使用file名称参数接收
    • springnvc把前端上传的文件封装成一个个CommonsMultipartFile
    • @param files
    • @param request
    • @return
      */
      public String upload(@RequestParam(value="file") CommonsMultipartFile[] files,
      HttpServletRequest request) {

      List> paths = new ArrayList>();
      //接收后台传输的保存文件的文件夹
      String filelSavePath = request.getParameter("filelSavePath");
      // 获取上传路径
      String path = "D:\imageTomcat\"+filelSavePath;

      for (CommonsMultipartFile file : files) {
      //获取文件名称
      String fileName = file.getOriginalFilename();
      //封装后的文件名称
      String saveFileName = getFileName(fileName);
      // 接收文件路径
      File targetFile = new File(path, saveFileName);
      // 保存
      try {
      file.transferTo(targetFile);
      } catch (Exception e) {
      e.printStackTrace();
      }
      Map map = new HashMap();
      map.put("path", filelSavePath+"/"+saveFileName);
      map.put("oldName", fileName);
      paths.add(map);
      }

      JSONArray arr = JSONArray.fromObject(paths);
      return arr.toString();
      }

      /**

    • 封装文件名

    • @param fileName 文件名

    • @return
      */
      public String getFileName(String fileName) {
      String suffix = fileName.substring(fileName.lastIndexOf("."), fileName.length());
      UUID uuid = UUID.randomUUID();
      return uuid+suffix;
      }

}

可以使用nginx缓存图片......