栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring-配置文件

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring-配置文件

pom配置


    4.0.0

    org.example
    ssmbuild
    1.0-SNAPSHOT

    
        8
        8
    


    
        
            junit
            junit
            4.13
            test
        
        
            mysql
            mysql-connector-java
            8.0.27
        
        
            com.mchange
            c3p0
            0.9.5.5
        
        
            javax.servlet
            servlet-api
            2.5
        
        
            javax.servlet.jsp
            jsp-api
            2.2
        
        
            javax.servlet
            jstl
            1.2
        

        
            org.mybatis
            mybatis
            3.5.2
        
        
            org.mybatis
            mybatis-spring
            2.0.6
        

        
            org.springframework
            spring-webmvc
            5.3.12
        
        
            org.springframework
            spring-jdbc
            5.1.9.RELEASE
        
        
            org.projectlombok
            lombok
            1.16.0
        

        
            org.aspectj
            aspectjweaver
            1.9.4
        

		
           com.fasterxml.jackson.core
           jackson-core
           2.12.5
       
       
           com.fasterxml.jackson.core
           jackson-databind
           2.12.5
       
       
           org.codehaus.jackson
           jackson-mapper-asl
           1.9.13
       
		

		
           commons-fileupload
           commons-fileupload
           1.3.3
       
       
           javax.servlet
           javax.servlet-api
           4.0.1
       
    


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

web.xml

配置springmvc





    
        springmvc
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:applicationContext.xml
        
        1
    
    
        springmvc
        /
    


    
        encodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            utf-8
        
    
    
        encodingFilter
        

//第一种方法
@RestController
public class FileController {
    @RequestMapping("/upload")
    public String fileupload(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) throws IOException {
        //获取文件名,file.getOriginalFilename();
        String uploadFileName = file.getOriginalFilename();

        //如果文件名为空,直接回到首页
        if ("".equals(uploadFileName)) {
            return "redirect:/index.jsp";
        }
        System.out.println("上传文件名:" + uploadFileName);

        //上传路径保存设置,javax-servlet包版本要在3.0以上
        String path = request.getServletContext().getRealPath("/upload");

        //如果路径不存在,创建一个
        File realPath = new File(path);
        if (!realPath.exists()) {
            realPath.mkdir();
        }
        System.out.println("上传文件保存地址" + realPath);

        InputStream is=file.getInputStream();//文件输入流
        OutputStream os=new FileOutputStream(new File(realPath,uploadFileName));//文件输出流

        //读取写出
        int len=0;
        byte[] buffer=new byte[1024];
        while((len=is.read(buffer))!=-1){
            os.write(buffer,0,len);
            os.flush();
        }

        os.close();
        is.close();
        return "redirect:index.jsp";
    }
    
    //第二种方法
    @RequestMapping("/upload2")
    public String fileUpload2(@RequestParam("file") CommonsMultipartFile file,HttpServletRequest request) throws IOException {
        String path=request.getServletContext().getRealPath("/upload");
        File realPath=new File(path);
        if(!realPath.exists()){
            realPath.mkdir();
        }

        System.out.println("上传文件保存地址"+realPath);
        file.transferTo(new File(realPath+"/" + file.getOriginalFilename()));

        return "redirect:/index.jsp";
    }
}

//下载
 @RequestMapping("/download")
    public String downloads(HttpServletResponse response,HttpServletRequest request) throws IOException {
        String path=request.getServletContext().getRealPath("/upload");
        String fileName="基础语法.jpg";

        //1.设置response响应头
        response.reset();//设置页面不缓存,清空buffer
        response.setCharacterEncoding("UTF-8");//字符编码
        response.setContentType("multipart/form-data");
        response.setHeader("Content-Disposition","attachment;fileName="+ URLEncoder.encode(fileName,"UTF-8"));

        File file=new File(path,fileName);
        //2.读取文件--输入流
        InputStream input=new FileInputStream(file);
        OutputStream out=new response.getOutputStream();

        byte[] b=new byte[1024];
        int index=0;
        while((index=input.read(b))!=-1){
            out.write(b,0,index);
            out.flush();
        }
//        out=null;
//        response.flushBuffer();
        out.close();

        input.close();
        return null;
    }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/735777.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号