Spring文件上傳配置

增長依賴jar包html

        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>

修改springmvc.xml配置文件,添加java

    <!-- 啓用文件上傳支持 -->
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="100000" />
        <property name="maxInMemorySize" value="10240" />
    </bean>

controller代碼web

package com.test.controller;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import net.sf.json.JSONObject;

@Controller
public class ServiceDBController {

	private static Log log = LogFactory.getLog(ServiceDBController.class);
	

	@RequestMapping(value="test.do")
	public void test(@RequestParam("myFile")MultipartFile myFile) {
         //處理文件
	}
}

表單spring

<form action="${pageContext.request.contextPath}/test.do" method="post" id="frm-uploadfile" enctype="multipart/form-data">
    <input type="file" name="myFile" /> <!--注意這個name要和對應的spring的controller參數名稱相同-->
    <input type="submit" value="上傳文件">
</form>
相關文章
相關標籤/搜索