spring单文件上传demo和下载代码

本文介绍了一个简单的文件上传和下载功能实现方法。通过JSP页面进行文件的选择与上传,并使用Spring MVC框架下的控制器来处理文件上传请求及后续的文件保存操作。此外,还实现了文件的下载功能,包括设置下载文件名及响应头。

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

UploadFiled.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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>
    <h2>文件上传</h2>
    <form action="upload.do" enctype="multipart/form-data" method="post">
        <table>
            <tr>
                <td>文件描述:</td>
                <td><input type="text" name="description"></td>
            </tr>
            <tr>
                <td>请选择文件:</td>
                <td><input type="file" name="file"></td>
            </tr>
            <tr>
                <td><input type="submit" value="上传"></td>
            </tr>
        </table>
    </form>
</body>
</html>

控制器FileUploadController.class

package cn.upload.controller;

import java.io.File;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import cn.upload.domain.User;

@Controller

public class FileUploadController {

	@RequestMapping("/UploadFiled")
	public String s(){
		System.out.println("==");
		return "/UploadFiled";
		
	}
	
//	@ResponseBody @RequestParam("description") String description,
	@RequestMapping(value="/upload",method=RequestMethod.POST)
	public String  upload(HttpServletRequest request,
						 @RequestParam("description")String description,
						 @RequestParam("file") MultipartFile file)throws Exception{
		System.out.println("sddddd");
		System.out.println(description);
		//如果文件不为空,写入上传路径
		if(!file.isEmpty()){
			//上传文件路径
			String path=request.getServletContext().getRealPath("/imag");
			System.out.println(path);
			String filename=file.getOriginalFilename();
			
			File filepath=new File(path,filename);
			//判断路径是否存在,如果不存在就创建一个
			if(!filepath.getParentFile().exists()){
				filepath.getParentFile().mkdirs();
				
			}
			//将上传的文件保存到目标文件中
			file.transferTo(new File(path+File.separator+filename));
			return "success";
		}else{
			return "error";
		}
		
	}
文件下载
@RequestMapping(value="/download")
	public ResponseEntity<byte[]> down(HttpServletRequest request,
			@RequestParam("filename") String filename,
			Model model)throws Exception{
		
		//下载路径
		String path=request.getServletContext().getRealPath("/imag/");
		
		File file=new File(path+File.separator+filename);
		HttpHeaders headers=new HttpHeaders();
		
		//下载显示的文件名,解决中文名称乱码问题
		String downloadfilename=new String(filename.getBytes("UTF-8"),"iso-8859-1");
		
		//通知浏览器以attachment (下载方式)打开图片
		headers.setContentDispositionFormData("attachment", downloadfilename);
		
		
		
		return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers,HttpStatus.CREATED);
	}

标题基于SpringBoot+Vue的学生交流互助平台研究AI更换标题第1章引言介绍学生交流互助平台的研究背景、意义、现状、方法与创新点。1.1研究背景与意义分析学生交流互助平台在当前教育环境下的需求及其重要性。1.2国内外研究现状综述国内外在学生交流互助平台方面的研究进展与实践应用。1.3研究方法与创新点概述本研究采用的方法论、技术路线及预期的创新成果。第2章相关理论阐述SpringBoot与Vue框架的理论基础及在学生交流互助平台中的应用。2.1SpringBoot框架概述介绍SpringBoot框架的核心思想、特点及优势。2.2Vue框架概述阐述Vue框架的基本原理、组件化开发思想及与前端的交互机制。2.3SpringBoot与Vue的整合应用探讨SpringBoot与Vue在学生交流互助平台中的整合方式及优势。第3章平台需求分析深入分析学生交流互助平台的功能需求、非功能需求及用户体验要求。3.1功能需求分析详细阐述平台的各项功能需求,如用户管理、信息交流、互助学习等。3.2非功能需求分析对平台的性能、安全性、可扩展性等非功能需求进行分析。3.3用户体验要求从用户角度出发,提出平台在易用性、美观性等方面的要求。第4章平台设计与实现具体描述学生交流互助平台的架构设计、功能实现及前后端交互细节。4.1平台架构设计给出平台的整体架构设计,包括前后端分离、微服务架构等思想的应用。4.2功能模块实现详细阐述各个功能模块的实现过程,如用户登录注册、信息发布与查看、在线交流等。4.3前后端交互细节介绍前后端数据交互的方式、接口设计及数据传输过程中的安全问题。第5章平台测试与优化对平台进行全面的测试,发现并解决潜在问题,同时进行优化以提高性能。5.1测试环境与方案介绍测试环境的搭建及所采用的测试方案,包括单元测试、集成测试等。5.2测试结果分析对测试结果进行详细分析,找出问题的根源并
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值