使用SpringMVC上傳文件

gbd8 9年前發布 | 4K 次閱讀 Java SpringMVC

package com.foo.controller;

import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest;

@Controller @RequestMapping(value="/file") public class FileUpload{

@RequestMapping(value="/gotofilepage")
public String gotofilepage(){
    System.out.println("進入到file中的gotofilepage方法中。。。。");

    return "uploadfile";
}

@RequestMapping(value="/fileupload")
public void fileUpload(HttpServletRequest request,
        HttpServletResponse response) {

    MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
    SimpleDateFormat dateformat = new SimpleDateFormat("yyyy/MM/dd/HH");
    /** 得到圖片保存目錄的真實路徑 **/
    String realpath = request.getSession().getServletContext().getRealPath("/");
    /** 構建圖片保存的目錄 **/
    String filedir ="files" + File.separator
            + dateformat.format(new Date());
    String filelocationdir = realpath + filedir;
    /** 根據真實路徑創建目錄 **/
    File logoSaveFile = new File(filelocationdir);
    if (!logoSaveFile.exists())
        logoSaveFile.mkdirs();
    /** 頁面控件的文件流 **/
    MultipartFile multipartFile = multipartRequest.getFile("file");
    /** 獲取文件的后綴,防止別人傳可執行文件 **/
    String suffix = multipartFile.getOriginalFilename().substring(
            multipartFile.getOriginalFilename().lastIndexOf("."));

    String imagename = multipartFile.getOriginalFilename();
    /** 拼成完整的文件保存路徑加文件 **/
    String filename = filelocationdir + File.separator + imagename;
    File file = new File(filename);

    try {
        multipartFile.transferTo(file);
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}</pre>

uploadfile.jsp


<body>
<h1>文件上傳</h1>
<form action="./fileupload.action" method="post" enctype="multipart/form-data">         
<input type="file" name="file" />
<input type="submit" value="upload" />
</form>
</body>



springmvc-servlet.xml



<?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:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:task="http://www.springframework.org/schema/task" 
    xsi:schemaLocation="
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">

    <!-- @Controller, @Service, @Configuration, etc. -->
    <context:component-scan base-package="com.foo.controller" />    

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>
    <!-- 上傳文件必須加 -->
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- set the max upload size100MB -->
        <property name="maxUploadSize">
            <value>104857600</value>
        </property>
        <property name="maxInMemorySize">
            <value>4096</value>
        </property>
    </bean>
<!--     <task:annotation-driven/>
    <bean id="taskTest" class="com.htf.task.MyTask"></bean>
    <task:scheduled-tasks>
          <task:scheduled ref="taskTest" method="say" cron="5/3 * * * * ?" />  
    </task:scheduled-tasks>
    <context:component-scan base-package="com.htf.task" /> -->
</beans>

需要jar

springmvc包

commons-io-2.2.jar

commons-fileupload-1.3.1.jar


 本文由用戶 gbd8 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!