原理:  从客户端上传到服务器                照片——文件夹——数据库

例如:桌面一张照片,在tomacat里创建upload文件夹,把桌面照片上传到upload文件夹里,并且把照片的名字取出来,取完名字把这个名字插入到数据库里面,下次要想取就取这个名字到upload文件夹下面去寻找这个照片,找到以后写相对路径,就可以在页面上显示照片。

所以我数据库的类型是照片的路径是varchar字符串类型

注:tomacat服务器是用eclipse敲代码的开发工具启动的,每一个都会把最新的源代码覆盖原有的所有代码,所有tomacat重启后照片会消失(真正的开发是编码和服务器不在一起不会出现以上情况)

例如:我要上传课程信息(里面包括照片)

1.上传课程信息jsp页面uploadcourse.jsp

<body background="image/zzz.jpg">
<div id="zt" style="height:960px;width:960px">
<div id="upld" style="height:300px;width:300px;margin-left: 300px;margin-top: 100px;">
<table>
<form action="CourseServlet?method=upload" method="post" enctype="multipart/form-data">
<tr>
<td style="text-align:center;"colspan="2"><font size="5">上传课程</font></td>
<tr height="40px">
<td><div style="width:100px">课程名称:<div></td>
<td> <input type="text" name="name" id="name"></td>
</tr>
<tr height="40px">
<td>课程描述:</td>
<td><input type="text" name="detail" id="detail"></td>
</tr>
<tr height="40px">
<td>封面图片:</td>
<td><input type="file" name="picture" id="picture"></td>
</tr>
<td height="40px">课程讲师:</td>
<td><input type="text" name="teacher" id="teacher"></td>
</tr>
<tr height="40px">
<td><input type="reset" value="重置"></td>
<td colspan="2" style="padding-left: 120px;"><input type="submit" value="提交"></td>
</tr>
</form>
</table>
</div>
</div>
</body>

2. 点击查看已上传课程按钮后触发活动xyadmin.jsp

<div style="height:100px"><a href="CourseServlet?method=displayCourse"  target="middle">查看已上传课程</a></div>

3.显示课程信息jsp页面displayCourse.jsp

<table border="0"cellspacing="0" cellpadding="0">
<tr>
<td style="width:50px;text-align: center">序号</td>
<td style="width:100px;text-align: center">课程名</td>
<td style="text-align: center">课程讲述</td>
<td style="text-align: center">封面图片</td>
<td style="width:100px;text-align: center">课程讲师</td>
<td style="width:100px;text-align: center">相关视频</td>
</tr>
<c:forEach items="${list_displaycourse}" var="course" varStatus="i">
<tr style="background:#7FFFD4">
<form action="CheckVideoServlet?method=checkvideo" method="post" target="middle">
<td style="width:50px;text-align: center">${i.count} </td>
<input type="hidden" name="c_id" id="c_id" value="${course.c_id} ">
<td style="width:100px;text-align: center">${course.c_name}</td>
<td style="text-align: center"><font style="font-size:12px;">${course.c_detail}</font></td>
<td style="text-align: center"><img width="100px" height="50px" src="upload/${course.c_picture}"/></td>
<td style="width:100px;text-align: center">${course.c_teacher}</td>
<td style="text-align: center"><input type="submit" value="查看"></td>
</form>
</tr>
</c:forEach>

后台CourseServlet(包含上传课程信息与显示课程信息两个处理逻辑)

public void upload(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String filename = null;
// 获得磁盘文件条目工厂
DiskFileItemFactory factory = new DiskFileItemFactory();
// 获取文件需要上传到的路径
String path = request.getRealPath("/upload");

// 如果没以下两行设置的话,上传大的 文件 会占用 很多内存,
// 设置暂时存放的 存储室 , 这个存储室,可以和 最终存储文件 的目录不同
/**
* 原理 它是先存到 暂时存储室,然后在真正写到 对应目录的硬盘上, 按理来说 当上传一个文件时,其实是上传了两份,第一个是以 .tem
* 格式的 然后再将其真正写到 对应目录的硬盘上
*/
factory.setRepository(new File(path));
// 设置 缓存的大小,当上传文件的容量超过该缓存时,直接放到 暂时存储室
factory.setSizeThreshold(1024 * 1024);

// 高水平的API文件上传处理
ServletFileUpload upload = new ServletFileUpload(factory);
InputStream in =null;
byte[] buf=null;//字节数组表示照片
try {
// 可以上传多个文件
List<FileItem> list = (List<FileItem>) upload.parseRequest(request);

for (FileItem item : list) {
// 获取表单的属性名字
String name = item.getFieldName();// title

// 如果获取的 表单信息是普通的 文本 信息
if (item.isFormField()) {
// 获取用户具体输入的字符串 ,名字起得挺好,因为表单提交过来的是 字符串类型的,表示表单的普通文本,如下拉列表,文本框,密码框等
String value = item.getString("UTF-8");// title content,设置格式防止出现乱码不匹配的情况
request.setAttribute(name, value);
}
// 对传入的非 简单的字符串进行处理 ,比如说二进制的 图片,电影这些
else {
/**
* 以下三步,主要获取 上传文件的名字,表示文本是上传控件
* 名字采用随机的方式设置的
*/
// 获取路径名
String value = item.getName();
String suffix = value.substring(value.lastIndexOf("."));
filename = "pro"+String.valueOf(((new Date()).getTime())%10000000)+suffix;
request.setAttribute(name, filename);


// 真正写到磁盘上
// 它抛出的异常 用exception 捕捉

// item.write( new File(path,filename) );//第三方提供的

// 手动写的,是将我电脑里的照片写在我服务器建立的upload文件夹下下面
OutputStream out = new FileOutputStream(new File(path,
filename));

in = item.getInputStream();

int length = 0;
buf = new byte[1024];//读1024个字节

System.out.println("获取上传文件的总共的容量:" + item.getSize());

// in.read(buf) 每次读到的数据存放在 buf 数组中
while ((length = in.read(buf)) != -1) {
// 在 buf 数组中 取出数据 写到 (输出流)磁盘上
out.write(buf, 0, length);

}

in.close();
out.close();
}
}

} catch (FileUploadException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
//以上servlet代码除了红字外其余都不变

String name=request.getAttribute("name").toString();
String detail=request.getAttribute("detail").toString();
String teacher=request.getAttribute("teacher").toString();

Course course=new Course();
course.setC_name(name);
course.setC_detail(detail);
course.setC_teacher(teacher);
course.setC_picture(filename);//核心代码,把filename的字符串给video.setImages插入数据库
boolean flag= courseService.uploadCourse(course);
if(flag){//上传成功
request.getRequestDispatcher("xyadmin.jsp").forward(request, response);
}else{
request.getRequestDispatcher("uploadcourse.jsp").forward(request, response);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String method=request.getParameter("method");
if("upload".equals(method)){
upload(request,response);
}else if("displayCourse".equals(method)){
List<Course> listCourse=courseService.displayCourse();
request.setAttribute("list_displaycourse", listCourse);
request.getRequestDispatcher("displayCourse.jsp").forward(request, response);
}
}

内容来源于网络如有侵权请私信删除
你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!