JSP实现图片上传并保存到数据库
开发工具与关键技术:Eclipse Java
撰写时间:2020年9 月 28 日
今天分享一个jsp页面图片上传的案例demowebs
准备工作: 准备依赖包:commons-fileupload-1.3.2.jar、commons-io-2.5.jar
,并将准备的包存放在/项目名称/WebContent/WEB-INF/lib
文件夹中。
接着在jsp页面添加form表单,表单用post提交,注意 enctype 属性值还要设置为enctype="multipart/form-data"
,在上传单个文件的时候,应该使用单个带有属性type="file"
的 <input .../>
标签。有些时候为了允许多个文件上传,就会用多个name属性值不同的input标签来实现,输入标签具有不同的名称属性的值,浏览器会为每个input标签关联一个【浏览】按钮。
jsp代码:
<div class="col-md-12">
<div class="col-md-6 col-md-offset-3">
<form id="formUserInsert" action="${ctx}/servlet/usersServlet?fun=doInsert" class="form-horizontal form-radius" method="post" enctype="multipart/form-data">
<input type="reset" name="reset" style="display: none;"/>
<div class="form-group form-group-sm">
<label class="control-label col-sm-3" for="userName">用户名</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="userName" name="userName">
</div>
</div>
<div class="form-group form-group-sm">
<label class="control-label col-sm-3" for="password">密码</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="password" name="password">
</div>
</div>
<div class="form-group form-group-sm">
<label class="control-label col-sm-3" for="userTypeId">用户类型</label>
<div class="col-sm-9">
<select class="form-control" id="userTypeId" name="userTypeId" >
<c:forEach items="${userTypes}" var="userType">
<option value="${userType.userTypeId}">${userType.userType}</option>
</c:forEach>
</select>
</div>
</div>
<div class="form-group form-group-sm">
<label class="control-label col-sm-3" for="sex">性别</label>
<div class="col-sm-9">
<select class=