1. 程式人生 > >檔案上傳與下載的簡單案例

檔案上傳與下載的簡單案例

檔案上傳

 

 

 

同時構建這個controller的過程中遇到最大的一個bug是:

得構建一個檢視資源管理器,因為controller中必須得認識ModelAndView,String型別

沒有ModelAndView物件,不知道如何去找視圖,會自動給你預設的檢視/upload,最後會使用檢視解析器幫我們返回的地址新增字首資訊和字尾資訊,/WEB-INF/upload.jsp.

配置檢視解析器:(可以再spring-webmvc-4.1.2.RELEASE-sources.jar/org/springframework/web/servlet/DispatcherServlet.properties

中的org.springframework.web.servlet.ViewResolver)

 

 

 

 

 

檔案下載

 

 

 

檔案具體格式

 

 

 

具體程式碼:

appilication.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:p="http://www.springframework.org/schema/p"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jee="http://www.springframework.org/schema/jee"

xmlns:mvc="http://www.springframework.org/schema/mvc

"

xsi:schemaLocation="

http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/jeehttp://www.springframework.org/schema/jee/spring-jee.xsd

http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd">

<context:component-scan base-package="uploadfile"></context:component-scan>

<mvc:annotation-driven></mvc:annotation-driven>

<mvc:default-servlet-handler></mvc:default-servlet-handler>

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

<property name="maxUploadSize" value="1048576"></property>

</bean>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="suffix" value=".jsp"></property>

<property name="prefix" value="/WEB-INF/"></property>

</bean>

</beans>

 

 

 

web.xml

<?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/javaeehttp://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"

version="4.0">

<servlet>

<servlet-name>uploadfile</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:application.xml</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>uploadfile</servlet-name>

<url-pattern>/</url-pattern>

</servlet-mapping>

</web-app>

 

 

 

uploadfile.jsp

<%--

Created by IntelliJ IDEA.

User: Lenovo

Date: 2018/11/23

Time: 13:05

To change this template use File | Settings | File Templates.

--%>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>

<head>

<title>Title</title>

</head>

<body>

<fieldset>

<legend>檔案上傳</legend>

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

<input type="file" name="file"/><br><br>

<input type="submit" name="btn" value="檔案上傳">

</form>

</fieldset>

</body>

</html>

 

 

 

 

upload控制器

package uploadfile;

 

import org.apache.commons.io.IOUtils;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.multipart.MultipartFile;

 

import javax.servlet.ServletOutputStream;

import javax.servlet.http.HttpServletResponse;

import java.io.*;

 

@Controller

public class upload {

@RequestMapping("/upload")

public void upload(MultipartFile file){

InputStream is=null;

OutputStream os=null;

try {

is=file.getInputStream();

os=new FileOutputStream(new File("D:/b.jsp"));

IOUtils.copy(is,os);

}catch (Exception e){

e.printStackTrace();

}finally {

if(is!=null){

try {

is.close();

} catch (IOException e) {

e.printStackTrace();

}finally {

if(os!=null){

try {

os.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

}

}

@RequestMapping("/download")

public void download(HttpServletResponse resp){

FileInputStream is=null;

try {

resp.setHeader("Content-Disposition","attachment;filename=a.jpg");

is=new FileInputStream(new File("D:/a.jpg"));

ServletOutputStream os=resp.getOutputStream();

IOUtils.copy(is,os);

}catch (Exception e){

e.printStackTrace();

}finally {

if(is!=null){

try {

is.close();

} catch (IOException e) {

e.printStackTrace();

}

};

}

}

}

 

 

執行結果為: