spring file upload and download

web.xml basic configuration:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>

        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </context-param>

        <servlet>
            <servlet-name>springmvc</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:springmvc.xml</param-value>
            </init-param>
        </servlet>
        <servlet-mapping>
            <servlet-name>springmvc</servlet-name>
            <url-pattern>*.do</url-pattern>
        </servlet-mapping>

      <!--解决中文乱码-->
          <filter>
              <filter-name>CharacterEncodingFilter</filter-name>
              <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
              <init-param>
                  <param-name>encoding</param-name>
                  <param-value>utf-8</param-value>
              </init-param>
          </filter>
          <filter-mapping>
          <filter-name>CharacterEncodingFilter</filter-name>
          <url-pattern>/*</url-pattern>
          </filter-mapping>

</web-app>

 jsp pages following code download and upload

upload:

<form action="upload.do" method="post" enctype="multipart/form-data">

  <%--用户名:<input name="username" > 密码:<input type="password" name="password">--%>
  文件 :<input type="file" name="file">
  <%--<input type="button" value="注册" >--%>
  <button>the Button</Submit>
</form>


download:
< A the href = "download.do?name = $ path {}" > File Download </ A >

springMVC (springmvcxml) and spring (applicationContext.xml) profile (spring in the main configuration file does not need to configure anything, as long as we create here is like, without management):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.aaa.springmvc.controller"></context:component-scan>
    <mvc:annotation-driven></mvc:annotation-driven>
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value=""></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

The controller code is as follows:

package com.aaa.springmvc.controller;

import com.aaa.spring.file.util.FileUtil;
import com.aaa.springmvc.entity.Users;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.UUID;

@Controller
public class UserControler {

    @RequestMapping("/upload")
    public String upload(Users users, MultipartFile file, Model model) {

        // really filename 
        String trueName = file.getOriginalFilename ();
        System.out.println(file.getName());
        //后缀名
        String suffix = trueName.substring(trueName.lastIndexOf("."));

        Path String = "D: / IMG /" ; 
     // use the UUID tools generate a unique file name for the file String savename
= UUID.randomUUID().toString(); NewFile File = new new File (path SAVENAME + + suffix); the try {
      // write the file file.transferTo(NewFile); }
catch (IOException e) { e.printStackTrace (); } //存储value model.addAttribute("path", path + savename + suffix); return "login"; } @RequestMapping("/download") public String download(String name, HttpServletResponse resp) { resp.setContentType("application/octet-stream"); resp.setCharacterEncoding ( "UTF-8" ); // name of the settings file resp.addHeader ( "Content-Disposition", "Attachment; filename =" + name); InputStream is = null; OutputStream os = null; try { is = new FileInputStream(name); os = resp.getOutputStream(); byte[] b = new byte[2048 * 10]; int len = 0; while ((len = is.read(b)) != -1) { os.write(b, 0, len); } os.flush(); } catch (Exception e) { e.printStackTrace (); } finally {         // close the file stream FileUtil.close(is, os); } return "success"; } @RequestMapping("/login") public String login() { return "index"; } }

 

Guess you like

Origin www.cnblogs.com/qurui1997/p/11073632.html