Spring完成文件上傳(示例代碼)。本站提示廣大學習愛好者:(Spring完成文件上傳(示例代碼))文章只能為提供參考,不一定能成為您想要的結果。以下是Spring完成文件上傳(示例代碼)正文
在現實開辟中,常常碰到要完成文件上傳到辦事器真個功效。Spring可以繼續commons-fileupload插件來完成文件上傳的功效。分為前端JSP編寫和後台Controller的編寫。
後期預備任務,起首要引入commons-fileupload這個jar包,pom.xml中的設置裝備擺設以下:
<!-- 完成文件上傳,spring集成了這個功效 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>
然後設置裝備擺設攔阻器,在dispatcher-servlet.xml中添加
<!-- 完成文件上傳,如許一旦某個Request是一個MultipartRequest,它就會起首被MultipartResolver處置,然後再轉發響應的Controller -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 設置上傳文件的最年夜尺寸為1MB -->
<property name="maxUploadSize">
<value>1048576</value>
</property>
</bean>
直接上代碼:
1、前端JSP編寫
upload.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script src="resources/js/jquery.js"></script>
</head>
<body>
<form method="post" action="upload.do" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" />
</form>
</body>
</html>
2、後台UploadController編寫
package nju.software.xkxt.web.controller;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
/**
* @author typ
*
*/
@Controller
public class UploadController {
/**
* 進入下載界面
*
* @param request
* @param response
* @param model
* @return
* @throws IOException
*/
@RequestMapping(value = "upload_enter.do", method = RequestMethod.GET)
public String enter(HttpServletRequest request,
HttpServletResponse response, ModelMap model) throws IOException {
// 進入下載界面
return "upload";
}
/**
* 詳細的完成下載功效
*
* @param request
* @param response
* @param model
* @throws IOException
*/
@RequestMapping(value = "upload.do", method = RequestMethod.POST)
public void upload(HttpServletRequest request,
HttpServletResponse response, ModelMap model) throws IOException {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
// 獲得上傳的文件
MultipartFile mFile = multipartRequest.getFile("file");
// 獲得上傳辦事器的途徑
String path = request.getSession().getServletContext()
.getRealPath("/WEB-INF/upload/");
// 獲得上傳的文件的文件名
String filename = mFile.getOriginalFilename();
InputStream inputStream = mFile.getInputStream();
byte[] b = new byte[1048576];
int length = inputStream.read(b);
path += "\\" + filename;
// 文件流寫到辦事器端
FileOutputStream outputStream = new FileOutputStream(path);
outputStream.write(b, 0, length);
inputStream.close();
outputStream.close();
}
}
如許就很輕易的完成了文件上傳的功效。