当前位置: 移动技术网 > IT编程>开发语言>Java > javaweb中上传图片并显示图片,用我要上传课程信息(里面包括照片)这个例子说明

javaweb中上传图片并显示图片,用我要上传课程信息(里面包括照片)这个例子说明

2019年04月08日  | 移动技术网IT编程  | 我要评论

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

例如:桌面一张照片,在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);
}
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网