springmvc文件上传下载

本文介绍如何在SpringMVC框架中实现文件的上传与下载功能,包括配置文件、控制器代码及页面实现。

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

在网上搜索的代码 参考整理了一份

需要使用的jar、

commons-fileupload.jar与commons-io-1.4.jar二个文件 1、表单属性为: enctype="multipart/form-data"

2、springmvc配置

<?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: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.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-3.2.xsd
  http://www.springframework.org/schema/mvc
  http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
  
  
<!-- 扫描包 -->
<context:component-scan base-package="com.ai.customer" />

 <!-- 启动注解 -->
 <mvc:annotation-driven />


<!-- 文件上传 -->
 <bean id="multipartResolver"  
   class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
   <!-- 设置上传文件的最大尺寸为10MB -->  
   <property name="maxUploadSize">  
       <value>10000000</value>  
   </property>  
  </bean>  


<!--  静态文件访问 -->
 <mvc:default-servlet-handler/> 
 <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 --> 
 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
 
 	<property name="prefix" value="/"/>
 	<property name="suffix" value=".jsp"/>	
 </bean> 
 
</beans>

2、上传下载功能代码

package com.ai.customer.controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class FileUploadController {

  /*
   * SpringMVC中的文件上传
   * @第一步:由于SpringMVC使用的是commons-fileupload实现,故将其组件引入项目中
   * @这里用到的是commons-fileupload-1.2.1.jar和commons-io-1.3.2.jar
   * @第二步:spring-mvx中配置MultipartResolver处理器。可在此加入对上传文件的属性限制
   *  <bean id="multipartResolver"  
   *  class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
   *	 <!-- 设置上传文件的最大尺寸为10MB -->  
   *		<property name="maxUploadSize">  
   *			<value>10000000</value>  
   *		 </property>  
   * </bean> 
   * 第三步:在Controller的方法中添加MultipartFile参数。该参数用于接收表单中file组件的内容
   *第四步:编写前台表单。注意enctype="multipart/form-data"以及<input type="file" name="****"/>
   *  如果是单个文件 直接使用MultipartFile 即可
   */ 

  @RequestMapping("/upload.do")
  public ModelAndView upload(String name,
      //上传多个文件
      @RequestParam("file") MultipartFile[] file,
      HttpServletRequest request) throws IllegalStateException,
      IOException {
    
    //获取文件 存储位置
    String realPath = request.getSession().getServletContext()
        .getRealPath("/uploadFile");
    
    File pathFile = new File(realPath);
    
    if (!pathFile.exists()) {
      //文件夹不存 创建文件
      pathFile.mkdirs();
    }
    for (MultipartFile f : file) {
      
      System.out.println("文件类型:"+f.getContentType());
      System.out.println("文件名称:"+f.getOriginalFilename());
      System.out.println("文件大小:"+f.getSize());
      System.out.println(".................................................");
      //将文件copy上传到服务器
      f.transferTo(new File(realPath + "/" + f.getOriginalFilename()));
       //FileUtils.copy
    }
    //获取modelandview对象
    ModelAndView view = new ModelAndView();
    view.setViewName("redirect:index.jsp");
    return view;
  }
  
  
  @RequestMapping(value = "download.do")  
  public ModelAndView download(HttpServletRequest request,  
      HttpServletResponse response) throws Exception {  
  
//		String storeName = "Spring3.xAPI_zh.chm";  
    String storeName="房地.txt";
    String contentType = "application/octet-stream";  
    FileUploadController.download(request, response, storeName, contentType);  
    return null;  
  }  
  
  
  //文件下载 主要方法
  public static void download(HttpServletRequest request,  
      HttpServletResponse response, String storeName, String contentType
       ) throws Exception {  
    
    request.setCharacterEncoding("UTF-8");  
    BufferedInputStream bis = null;  
    BufferedOutputStream bos = null;  
  
    //获取项目根目录
    String ctxPath = request.getSession().getServletContext()  
        .getRealPath("");  
    
    //获取下载文件露肩
    String downLoadPath = ctxPath+"/uploadFile/"+ storeName;  
  
    //获取文件的长度
    long fileLength = new File(downLoadPath).length();  

    //设置文件输出类型
    response.setContentType("application/octet-stream");  
    response.setHeader("Content-disposition", "attachment; filename="  
        + new String(storeName.getBytes("utf-8"), "ISO8859-1")); 
    //设置输出长度
    response.setHeader("Content-Length", String.valueOf(fileLength));  
    //获取输入流
    bis = new BufferedInputStream(new FileInputStream(downLoadPath));  
    //输出流
    bos = new BufferedOutputStream(response.getOutputStream());  
    byte[] buff = new byte[2048];  
    int bytesRead;  
    while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {  
      bos.write(buff, 0, bytesRead);  
    }  
    //关闭流
    bis.close();  
    bos.close();  
  }  
   
}

3、上传页面

<%@ 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>Insert title here</title>


</head>
<body>


  <form action="upload.do" method="post" enctype="multipart/form-data">
  
    <input type="text" name="name" />
    <br>
    <input type="file" name="file">
    <br>
    <input type="file" name="file" />
    
    <input type="submit" value="提交">
  </form>


</body>
</html>
转载自http://www.tuicool.com/articles/nMVjaiF

4、下载直接访问控制器如:http:\\localhost:8080/springmvc/download.do

内容概要:本文详细介绍了基于FPGA的144输出通道可切换电压源系统的设计与实现,涵盖系统总体架构、FPGA硬件设计、上位机软件设计以及系统集成方案。系统由上位机控制软件(PC端)、FPGA控制核心和高压输出模块(144通道)三部分组成。FPGA硬件设计部分详细描述了Verilog代码实现,包括PWM生成模块、UART通信模块和温度监控模块。硬件设计说明中提及了FPGA选型、PWM生成方式、通信接口、高压输出模块和保护电路的设计要点。上位机软件采用Python编写,实现了设备连接、命令发送、序列控制等功能,并提供了一个图形用户界面(GUI)用于方便的操作和配置。 适合人群:具备一定硬件设计和编程基础的电子工程师、FPGA开发者及科研人员。 使用场景及目标:①适用于需要精确控制多通道电压输出的实验环境或工业应用场景;②帮助用户理解和掌握FPGA在复杂控制系统中的应用,包括PWM控制、UART通信及多通道信号处理;③为研究人员提供一个可扩展的平台,用于测试和验证不同的电压源控制算法和策略。 阅读建议:由于涉及硬件和软件两方面的内容,建议读者先熟悉FPGA基础知识和Verilog语言,同时具备一定的Python编程经验。在阅读过程中,应结合硬件电路图和代码注释,逐步理解系统的各个组成部分及其相互关系。此外,实际动手搭建和调试该系统将有助于加深对整个设计的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值