用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("");
本文代码也是参考其他网页编写然后进行个人总结并分享给大家,如果有不对的地方还请大佬指正
💖:再小的帆也能远航