基于ssm框架上传下载的实现

一、相关配置

框架应该已经搭建好了,所以这里不再复述。在ssm框架的基础上配置下面的内容。
1.pom.xml的配置

<properties>
   <commons-fileupload.version>1.3.1</commons-fileupload.version>
   <commons-io.version>2.4</commons-io.version>
</properties>

    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>${commons-io.version}</version>
    </dependency>

    <dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>${commons-fileupload.version}</version>
      <exclusions>
        <exclusion>
          <groupId>commons-io</groupId>
          <artifactId>commons-io</artifactId>
        </exclusion>
      </exclusions>
    </dependency>

2.spring-mvc.xml中的配置

<!-- 定义文件上传解析器 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!-- 设定默认编码 -->
        <property name="defaultEncoding" value="UTF-8" />
        <!-- 设定文件上传的最大值5MB,5*1024*1024 -->
        <property name="maxUploadSize" value="5242880" />
        <property name="maxInMemorySize" value="4096" />
    </bean>

二、上传的实现

1.jsp页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>上传页面</title>
</head>
<body>
        <form method="post" action="file/upload" enctype="multipart/form-data">
            <p><span>文件:</span>
                <input type="file" name="file">
            </p>

            <p><input type="submit" value="提交"></p>
        </form>
</body>
</html>

代码:

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.UUID;

@Controller
@RequestMapping("/file")
public class FileUpload{
    @RequestMapping("/upload")
    public void upload(@RequestParam("file")MultipartFile file, HttpServletRequest request,
                         HttpServletResponse response, ModelMap model) throws Exception {
        //获取原始文件名
        String fileName=file.getOriginalFilename
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
基于SSM框架的文件上下载代码,可以分为两部分:前端页面和后端代码。 前端页面: 1. 文件上页面(upload.jsp) ``` <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>文件上</title> </head> <body> <form method="post" action="fileUpload" enctype="multipart/form-data"> <input type="file" name="file"/> <input type="submit" value="上"/> </form> </body> </html> ``` 2. 文件下载页面(download.jsp) ``` <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>文件下载</title> </head> <body> <table> <thead> <tr> <th>文件名</th> <th>大小</th> <th>下载</th> </tr> </thead> <tbody> <c:forEach items="${files}" var="file"> <tr> <td>${file.name}</td> <td>${file.size}</td> <td><a href="fileDownload?fileName=${file.name}">下载</a></td> </tr> </c:forEach> </tbody> </table> </body> </html> ``` 后端代码: 1. 文件上Controller(FileUploadController.java) ``` @Controller public class FileUploadController { @RequestMapping(value = "/fileUpload", method = RequestMethod.POST) @ResponseBody public String fileUpload(@RequestParam("file") MultipartFile file) { if (file.isEmpty()) { return "请选择文件"; } String fileName = file.getOriginalFilename(); String filePath = "C:\\upload\\"; File dest = new File(filePath + fileName); try { file.transferTo(dest); return "文件上成功"; } catch (IOException e) { return "文件上失败"; } } } ``` 2. 文件下载Controller(FileDownloadController.java) ``` @Controller public class FileDownloadController { @RequestMapping(value = "/fileDownload") public void fileDownload(HttpServletRequest request, HttpServletResponse response, @RequestParam("fileName") String fileName) { String filePath = "C:\\upload\\"; File file = new File(filePath + fileName); if (file.exists()) { response.setContentType("application/octet-stream"); response.setContentLength((int) file.length()); response.setHeader("Content-Disposition", "attachment;filename=" + fileName); try { FileInputStream in = new FileInputStream(file); OutputStream out = response.getOutputStream(); byte[] buffer = new byte[1024]; int len; while ((len = in.read(buffer)) > 0) { out.write(buffer, 0, len); } in.close(); out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); } } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值