w3shool html上传文件,文件上传

文件上传

Spring MVC下的处理是类似的。

下面展示一个简单的实现。

项目结构与源码

84b10c8fc779c08b33d4052e97f6726b.png

web.xml

contextConfigLocation

/WEB-INF/applicationContext.xml

org.springframework.web.context.ContextLoaderListener

dispatcher

org.springframework.web.servlet.DispatcherServlet

2

1000000

2000000

4000000

dispatcher

/

30

redirect.jsp

注意其中的限制文件大小的配置:

1000000

2000000

4000000

applicationContext.xml

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

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

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

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

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

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

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

dispatcher-servlet.xml

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

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

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

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

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

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

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

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

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd

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

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

class="org.springframework.web.multipart.support.StandardServletMultipartResolver">

indexController

class="org.springframework.web.servlet.view.InternalResourceViewResolver"

p:prefix="/WEB-INF/jsp/"

p:suffix=".jsp" />

class="org.springframework.web.servlet.mvc.ParameterizableViewController"

p:viewName="index" />

注意,这里增加了一个multipart解析器StandardServletMultipartResolver,用来处理上传的文件。/static是存放静态资源的目录,

我们也准备将上传的文件放到这个目录里。

UploadedFile.java

package me.letiantian.form;

import org.springframework.web.multipart.MultipartFile;

public class UploadedFile {

private String fileName;

private MultipartFile multipartFile;

public String getFileName() {

return fileName;

}

public void setFileName(String fileName) {

this.fileName = fileName;

}

public MultipartFile getMultipartFile() {

return multipartFile;

}

public void setMultipartFile(MultipartFile multipartFile) {

this.multipartFile = multipartFile;

}

}

HelloController.java

package me.letiantian.controller;

import java.io.File;

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;

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

import me.letiantian.form.UploadedFile;

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

import org.springframework.web.multipart.MultipartFile;

@Controller

@RequestMapping("/hello")

public class HelloController{

@RequestMapping(value = "")

public String index() {

return "hello/index";

}

@RequestMapping(value = "/upload")

public void output(@ModelAttribute UploadedFile uploadedFile,

HttpServletRequest request,

HttpServletResponse response) throws IOException {

response.setContentType("text/html;charset=UTF-8");

PrintWriter out = response.getWriter();

MultipartFile multiPartFile = uploadedFile.getMultipartFile();

System.out.println("文件原始名称:"+multiPartFile.getOriginalFilename());

System.out.println("表单给定的文件名称:"+uploadedFile.getFileName());

try{

System.out.println("上传目录:"+request.getServletContext().getRealPath("/static"));

File file = new File(request.getServletContext().getRealPath("/static"),

uploadedFile.getFileName());

multiPartFile.transferTo(file); // 将文件写入本地

out.println("

上传成功

");

} catch (Exception ex) {

System.out.println(""+ex.getMessage());

out.println("

上传失败

");

}

}

}

注意,表单数据被绑定到了uploadedFile对象中。

hello/index.jsp

JSP Page

指定文件名:

选择文件:

测试程序

选择文件,指定名称:

f4a1c724f0be02982e6b2e8f58c778b5.png

上传成功:

c7c5ad7e98f13ba8be948e9c4e383adf.png

查看上传的文件:

3ebe570a3bac86b2e67c58d1386484a2.png

Tomcat输出:

文件原始名称:01.png

表单给定的文件名称:1.png

上传目录:/data/Code/netbeans/Project_0109/build/web/static

资料

《Spring MVC学习指南》 第11章

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值