SSM框架文件上传到本地

3 篇文章 0 订阅
用jsp和Controller实现文件上传到本地(本文写的绝对地址)

首先我们在pom.xml中导入依赖

<!--文件上传下载-->
<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3.1</version>
</dependency>

然后是我的jsp页面代码

<form action="${pageContext.request.contextPath}/uploadwenjian" method="post" enctype="multipart/form-data">
	<li><span class="wen">文档标题</span><input required name="title" type="text" placeholder="请输入文档标题" class="text1"></li>
	<li><span class="wen">文档类型</span>
		<select required name="type" style="outline: 1px solid #dbdbdb;">
			<option value ="">==请选择类型==</option>
			<option value ="outline">教学大纲</option>
			<option value ="course">课程计划</option>
			<option value="talent">人才培训计划</option>
			<option value="kejian">课件</option>
		</select>
	</li>
	<li style="height: 300px"><span class="wen">文档描述</span><input required name="miaoshu" type="textarea" style="width:300px;height: 300px;"></li>
	<li><span class="wen">上传者</span>
		<select required name="people" style="outline: 1px solid #dbdbdb;">
			<option value ="">==请选择上传者==</option>
			<!-- 我这里是在Controller中获取了一个表中的数据然后在这里循环出来 -->
			<c:forEach items="${teachers}" var="teacher">
				<option value ="${teacher.teachername}">${teacher.teachername}</option>
			</c:forEach>
		</select>
	</li>
	选择文件:<input required type="file" name="file" width="120px">
	<li><input type="submit" class="but2" value="上传"></li>
</form>

然后是我的Controller代码

	//文件存储
    @RequestMapping(value = "/uploadwenjian", method = RequestMethod.POST)
    public String uploadwenjian(String title, String type, String people, String miaoshu,
                                @RequestParam("file") MultipartFile file,
                                HttpServletRequest request, Model model) throws IOException {
        //这里是存储文件的路径
        String realPath = "C:/YTeacher";
        File path = new File(realPath);
        if (!path.exists()) {
            path.mkdir();
            System.out.println("创建了父级目录--->" + path);
        }
        //这里实例化了一个时间对象并获取当前时间存入数据库中  以得到文件上传时间
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  hh:mm:ss");
        String date1 = sdf.format(date);
        System.out.println("当前时间" + date1);
        String oldName = file.getOriginalFilename();
        System.out.println("文件名字_______++:" + oldName);
        //把文件放到项目指定地方
        String newfile = realPath + File.separator + people;
        File newpath = new File(newfile);
        if (!newpath.exists()) {
            newpath.mkdir();
            System.out.println("创建目录为:" + newpath.toString());
        }
        System.out.println("指定路径::" + realPath + File.separator + people + "\\" + oldName);
        File dest = new File(realPath + File.separator + people + "\\" + oldName);
        file.transferTo(dest);
        //这是演示向一个表中存入数据 outline表(大纲)
        if (type.equals("outline")) {
            Outline outline = new Outline();
            outline.setOutlinepeople(people);
            outline.setOutlinetitle(title);
            outline.setOutlinecontent(miaoshu);
            outline.setOutlinepath(people + "/" + oldName);
            outline.setOutlinedate(date1);
            int flag = outlineService.insertOutline(outline);
            if (flag == 1) {
                model.addAttribute("teachermsg", "上传成功");
            } else {
                model.addAttribute("teachermsg", "上传失败");
            }
            System.out.println(oldName + "是否成功----:" + flag); //flag为1代表上传成功
        }
        return "teachers/shangchuan";
    }

以上就是上传文件的步骤了,文件存储路径那里还可/获取项目动态绝对路径,具体代码如下
String path = request.getServletContext().getRealPath("");

本文代码也是参考其他网页编写然后进行个人总结并分享给大家,如果有不对的地方还请大佬指正

💖:再小的帆也能远航

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值