Spring学习(十一)spring mvc上传下载

如下示例:
页面:
web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

  <servlet>
    <servlet-name>etoak</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>etoak</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

  <display-name></display-name> 
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-a

etoakk-servlet.xml:

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

    <!-- 
        请求解析器 
            字符请求解析器 HandlerMapping
            字节请求解析器 MultipartResolver
            CommonsMultipartResolver
                当服务器端使用commons-fileupload处理上传请求时,使用该解析器
            StandardServletMultipartResolver 
                当服务器段使用smartupload处理上传请求时,使用该解析器

    注意:在注册上传请求解析器时,该解析器的名字(id)值必须是 : multipartResolver
            接口名-首字母小写
     -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean>

    <context:component-scan base-package="com"/>

</beans>

FileController.java:


@Controller
public class FileController {

    @RequestMapping("/upload")
    public String upload(User user , 
            HttpServletRequest request)throws Exception{

        MultipartFile myfile = user.getMyfile();

        String filename = myfile.getOriginalFilename();
        String contentType = myfile.getContentType();
        long size = myfile.getSize();

        // 从上传文件中获取一个输入流
        InputStream is = myfile.getInputStream();

        // 定位到file目录  request.session.ServletContext.getRealPath("/file")
        String path = request.getSession().getServletContext().getRealPath("/file");
        String newFilename = new UUIDGenerator().generate().toString()+
            filename.substring(filename.lastIndexOf("."));
        File file = new File(path+"/"+newFilename);

        OutputStream os = new FileOutputStream(file);

        int len;
        byte[] data = new byte[1024];
        while((len=is.read(data))!=-1)
            os.write(data, 0, len);
        is.close();
        os.close();

        return "redirect:success.jsp";

    }

    @RequestMapping("/download")
    public void download(String filename,HttpServletRequest request,HttpServletResponse response) throws Exception{
        System.out.println("文件名"+filename);
        String path = request.getSession().getServletContext().getRealPath("/file");
        File file = new File(path+"/"+filename);

        response.setContentType("multipart/form-data");
        response.setHeader("content-Disposition", "attachment;filename="+filename);
        InputStream is = new FileInputStream(file);
        OutputStream os = response.getOutputStream();
        int len;
        byte[] data = new byte[1024];
        while((len=is.read(data))!=-1)
            os.write(data, 0, len);
        is.close();
        os.close();
    }
}

User.java:

package com.etoak.bean;

import org.springframework.web.multipart.MultipartFile;

public class User {

    /** myfile  文件
     * spring-mvc如何封装文件类型对象
     * 
     * struts1 - FormFile
     * struts2 - File String String
     * spring-mvc  -  MultipartFile接口 、 CommonsMultipartFile
     */
    private MultipartFile myfile;

    public MultipartFile getMyfile() {
        return myfile;
    }

    public void setMyfile(MultipartFile myfile) {
        this.myfile = myfile;
    }

}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值