用tomcat搭建图片服务器(SSM)

将图片上传到Tomcat中的upload中,外部通过URL访问

1.项目结构

用tomcat搭建图片服务器(SSM)_第1张图片

2.FileController

package com.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.sql.SQLOutput;
import java.util.HashMap;
import java.util.UUID;

@Controller
public class FileController {
    @Value("${netAddress}")
    private String host;
    //提交修改页面 入参  为 Items对象
    @ResponseBody
    @RequestMapping(value = "/updateitem.action",method = RequestMethod.POST)
    public Object updateitem(@RequestParam("myPhoto") MultipartFile pictureFile, HttpServletRequest request) throws Exception{
        System.out.println("asd");
        HashMap hashMap=new HashMap<>();
        ObjectMapper mapper = new ObjectMapper();
        try {
            if (pictureFile!=null){
                //保存图片到
                String name = UUID.randomUUID().toString().replaceAll("-", "");
                //jpg
                String ext = FilenameUtils.getExtension(pictureFile.getOriginalFilename());
                /**
                 *  String path = request.getSession().getServletContext().getRealPath("/");
                 * path值如下:
                 * C:\develop\apache-tomcat-7.0.75\webapps\ROOT\
                 */
                String url = request.getSession().getServletContext().getRealPath("/upload");
                pictureFile.transferTo(new File(url+"/" + name + "." + ext));
                String pitureUrl=host+name + "." + ext;
                System.out.println(pitureUrl);
                hashMap.put("end","OK");
                hashMap.put("url",pitureUrl);
            }else {
                hashMap.put("end","do not find file---pictureFile is null");
            }


        }catch (Exception e){
            hashMap.put("end","upload err");
            e.printStackTrace();
        }

        System.out.println(hashMap.toString());
        return mapper.writeValueAsString(hashMap);
    }
}

3.appicationContext.xml





	
	


4.host.properties

netAddress=http://127.0.0.1:8080/springmvcphoto/upload/

5.springMvc




    

    
    

    
    
    
    
    
    

    
    

    
    
        
        
        
        
        
    


    
    
    

    
    
        
        
    

6.web.xml



    springmvc-01
    
        index.html
        index.htm
        index.jsp
        default.html
        default.htm
        default.jsp
    

    
        contextConfigLocation
        classpath:applicationContext.xml
    

    
    
        org.springframework.web.context.ContextLoaderListener
    

    
    
        encoding
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            UTF-8
        
    

    
        encoding
        *.action
    


    
    
        springmvc
        org.springframework.web.servlet.DispatcherServlet
        
        
            contextConfigLocation
            classpath:springmvc.xml
        
    

    
        springmvc
        
        *.action
    

7.pom.xml



    4.0.0

    com.test
    sringmvcphoto
    1.0-SNAPSHOT
    war
    
        UTF-8
        1.8
        1.8
        4.12
        4.2.4.RELEASE
        1.6.4
        2.4.2
        1.0.9
        4.3.5
        1.2
        2.5
        2.0
        2.5
        3.3.2
        2.4
        3.3
      
        0.9.1
        1.3.1
    

    
        
        
            commons-io
            commons-io
            2.4
        

        
        
            commons-fileupload
            commons-fileupload
            1.3.1
        


    
    
        com.fasterxml.jackson.core
        jackson-databind
        ${jackson.version}
    


    
    
        junit
        junit
        ${junit.version}
        test
    
    
    
        org.slf4j
        slf4j-log4j12
        ${slf4j.version}
    


    
    
        org.springframework
        spring-context
        ${spring.version}
    
    
        org.springframework
        spring-beans
        ${spring.version}
    
    
        org.springframework
        spring-webmvc
        ${spring.version}
    
    
        org.springframework
        spring-jdbc
        ${spring.version}
    
    
        org.springframework
        spring-aspects
        ${spring.version}
    

    
        org.springframework
        spring-context-support
        ${spring.version}
    
    
    
        jstl
        jstl
        ${jstl.version}
    
    
        javax.servlet
        servlet-api
        ${servlet-api.version}
        provided
    
    
        javax.servlet
        jsp-api
        ${jsp-api.version}
        provided
    

        
        
            com.fasterxml.jackson.core
            jackson-core
            2.9.7
        

        
        junit
        junit
        4.11
        test
    

    
    
        

            
                org.apache.tomcat.maven
                tomcat7-maven-plugin
                2.2
            
        

        
            
                src/main/resources
                
                    **/*.properties
                    **/*.xml
                    **/*.tld
                
                false
            
            
                src/main/java
                
                    **/*.properties
                    **/*.xml
                    **/*.tld
                
                false
            
        
    

8.upload文件夹所在位置用tomcat搭建图片服务器(SSM)_第2张图片9.更该配置文件中参数

用tomcat搭建图片服务器(SSM)_第3张图片

10.测试




    上传图片
    

	


 
文件:
 

你可能感兴趣的:(文件服务器)