1. 程式人生 > >java文件上傳 學習筆記

java文件上傳 學習筆記

實體 gets 工具 pack sch www. zab dsi ace

實體:
package cn.it.entity;

import java.io.Serializable;

import org.springframework.web.multipart.MultipartFile;

public class Student implements Serializable {

    private String stuName;
    private String stuPass;
    private MultipartFile [] files;
    public MultipartFile[] getFiles() {
        return files;
    }
    public void setFiles(MultipartFile[] files) {
        this.files = files;
    }
    public String getStuName() {
        return stuName;
    }
    public void setStuName(String stuName) {
        this.stuName = stuName;
    }
    public String getStuPass() {
        return stuPass;
    }
    public void setStuPass(String stuPass) {
        this.stuPass = stuPass;
    }
    @Override
    public String toString() {
        return "Student [stuName=" + stuName + ", stuPass=" + stuPass + "]";
    }

}

工具類

package cn.it.utils;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

@Component
public class FileUploadUtils {
    /*
     * 註入字符串,#{}為spel語言,其中uploadProperties,是xml配置文件中註入properties文件的bean id,
     *  path為properties文件的其中一個key ,也可以通過下邊的set方法註入
     */ 
    @Value("#{uploadProperties.path}") 
    private  String  path;

    //獲取文件的擴展名
    private  String getExtName(MultipartFile file){
        return FilenameUtils.getExtension(file.getOriginalFilename());
    }
    //創建文件的新名
    private  String createNewName(MultipartFile file){
        return UUID.randomUUID().toString()+"."+getExtName(file);
    }
    //
    public  String upload(MultipartFile file){
        String newName = null;
        try {
            newName =createNewName(file);
            System.out.println("path:"+path);
            FileUtils.copyInputStreamToFile(file.getInputStream(), new File(path+newName));

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();        
            newName = null;
        }
        return newName;
    }

}

配置文件

<?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"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
    ">
<!-- 
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
    <context:component-scan base-package="*"></context:component-scan>
    --> 

    <!-- mvc:annotation-driven,取代了上面的DefaultAnnotationHandlerMapping,AnnotationMethodHandlerAdapter
兩個Bean的配置   -->

<!-- 基於上面的示例,在spring3中可以進一步簡化配置,取代上面的註解方式.
    步驟如下
    1.使用上面的action類,仍然給類及方法添加@Controller(類)、@RequestMapping(類及方法)註解
    2.本文件頂部添加spring mvc 命名空間的信息(可以參考org.springframework.web.servlet.config包)
    3.添加下面註解驅動<mvc:annotation-driven>,取代了上面的DefaultAnnotationHandlerMapping,AnnotationMethodHandlerAdapter,
    並啟動了json的註解支持
     -->

    <mvc:annotation-driven></mvc:annotation-driven>
    <context:component-scan base-package="*"/>

    <!--文件上傳使用, 配置multipartResolver,id名為約定好的 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 配置文件(每次上傳的所有文件總大小)大小,單位為b, 1024000表示1000kb  -->
        <property name="maxUploadSize" value="102400000" />     
    </bean>
    <!--PropertiesFactoryBean對properties文件可用 ,可以用來註入properties配置文件的信息 -->
    <bean id="uploadProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="location" value="classpath:upload.properties"></property>
    </bean>

</beans>

頁面


<form action="student/add.action" method="post" enctype="multipart/form-data">
學生姓名:<input type="text" name="stuName"><br>
密碼:<input type="text" name="stuPass"><br>
請選擇文件:<input type="file" name="files"><br>
<input type="file" name="files"><br>
<input type="submit" value="add">
</form><hr>
<a href="student/add.action?stuName=吳文亮">測試get方式</a>
  </body>
</html>

java文件上傳 學習筆記