文件的上传与下载

本文介绍如何使用Spring MVC框架实现文件的上传与下载功能。通过配置依赖和multipartResolver,结合JSP页面与Servlet控制器,演示了具体的操作流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需要导入的依赖:

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

在spring中的applicationContext.xml中配置:

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="utf-8"></property>
        <property name="maxUploadSize" value="5242440"></property>
</bean>

index.jsp页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>文件上传下载</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/file/upload.do"
        method="post" enctype="multipart/form-data">
        选择文件:<input type="file" name="file" width="120px"> <input
        type="submit" value="上传">
</form>
<hr>
<form action="${pageContext.request.contextPath }/file/down.do"
      method="get">
    <input type="submit" value="下载">
</form>
</body>
</html>

${pageContext.request.contextPath }(提取所部署项目的名字的绝对路径)与 <%=request.getContextPath()%>等价,可以说是它的EL版本,意思就是取出部署的应用程序名或者是当前的项目名称 。
采用form表单提交的方式进行文件的上传与下载。/后面跟的是与controller中的方法连接的路径。

UploadTestServlet.java代码:

@Controller
@RequestMapping("file")
public class UploadTestServlet {

    @RequestMapping(value="/upload",method=RequestMethod.POST)
    @ResponseBody
    public String upload(MultipartFile file,HttpServletRequest request) throws IOException{
        String path = request.getSession().getServletContext().getRealPath("upload");
        String fileName = file.getOriginalFilename();
        File dir = new File(path,fileName);
        if(!dir.exists()){
            dir.mkdirs();
        }
        file.transferTo(dir);
        return fileName;
    }
    @RequestMapping("/down")
    public void down(HttpServletRequest request,HttpServletResponse response) throws Exception{
        String fileName = request.getSession().getServletContext().getRealPath("upload")+"/101.jpg";
        InputStream bis = new BufferedInputStream(new FileInputStream(new File(fileName)));
        String filename = "下载文件.jpg";
        filename = URLEncoder.encode(filename,"UTF-8");
        response.addHeader("Content-Disposition", "attachment;filename=" + filename);
        response.setContentType("multipart/form-data");
        BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
        int len = 0;
        while((len = bis.read()) != -1){
            out.write(len);
            out.flush();
        }
        out.close();
    }
}

String path = request.getSession().getServletContext().getRealPath(“upload”);
表示上传文件到的地址。
String fileName = request.getSession().getServletContext().getRealPath(“upload”) +"/101.jpg";表示在该路径下下载对应的101.jpg文件,也可以将它改为你想给别人下载的文件,接下来的是将文件以输入输出流的形式的代码。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值