文件上传(MultipartFile)

2021-04-22 22:29

阅读:575

YPE html>

标签:context   属性   lan   enc   line   ppi   名称   ring   com   

采用springmvc框架实现上传

jar包支持

技术图片

 

springMVC配置文件


    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

        package="com.atguigu">class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            default-servlet-handler/>
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
            

 

1. file.jsp

    pageEncoding="UTF-8"%>测试上传与下载

down
    
头像 : 描述 :

2.UploadController

package com.atguigu.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class UploadAndDown {
    @RequestMapping("/down")
    public ResponseEntitybyte[]> down(HttpSession session) throws Exception{
        //获取下载文件的路径 (获取tomcat服务器的位置)
        String realPath = session.getServletContext().getRealPath("img");
        String finalPath = realPath+ File.separator +"1.jpg";
        //创建字节输入流
        InputStream in = new FileInputStream(finalPath);
        //available():获取输入流所读取的文件的最大字节数
        byte[] body = new byte[in.available()];
        //把字节读取到数组中
        in.read(body);
        //设置请求头
        MultiValueMap headers = new HttpHeaders();
        headers.add("Content-Disposition", "attachment;filename=aaa.jpg");
        //设置响应状态
        HttpStatus statusCode = HttpStatus.OK;
        in.close();

        
        ResponseEntitybyte[]> entity = new ResponseEntitybyte[]>(body, headers, statusCode);
        return entity;
        
    }
    
    @RequestMapping(value="/up_old", method = RequestMethod.POST)
    public String up_old (String desc,MultipartFile uploadFile,HttpSession session) throws Exception {
        //获取上传文件的名称
        String fileName = uploadFile.getOriginalFilename();
        //获取上传文件的路径
        String path = session.getServletContext().getRealPath("photo")+File.separator+fileName;
        //获取输入流
        InputStream is = uploadFile.getInputStream();
        //获取输出流
        OutputStream os = new FileOutputStream(new File(path));
        //开始复制
        int i = 0;
        byte[] bytes = new byte[1024];
        while((i = is.read(bytes))!=-1) {
            os.write(bytes, 0, i);
        }
        os.close();
        is.close();
        return "success";
        
    }
    
    

/**
    写法二
* 使用springmvc实现上传,采用MultipartFile的transfer() * @param desc * @param uploadFile * @param session * @return * @throws Exception */ @RequestMapping(value="/up", method = RequestMethod.POST) public String up (String desc,MultipartFile uploadFile,HttpSession session) throws Exception { //获取上传文件的名称 String fileName = uploadFile.getOriginalFilename(); //解决文件重名问题 String finalFileName = UUID.randomUUID().toString()+fileName.substring(fileName.lastIndexOf(".")); //获取上传文件的路径 String path = session.getServletContext().getRealPath("photo")+File.separator+finalFileName; uploadFile.transferTo(new File(path)); return "success"; } }

 

文件上传(MultipartFile)

标签:context   属性   lan   enc   line   ppi   名称   ring   com   

原文地址:https://www.cnblogs.com/lsk-130602/p/12240710.html


评论


亲,登录后才可以留言!