文章目录
前言
之前实现了增删改查和登录功能,然后这次优化jsp里的java代码和实现上传功能!
要优化之前的源码,在此基础上面优化代码
一、导入jar包
这个是要写上传图片代码的jar包
二、重写上传图片代码
1.修改添加图片的jsp代码
找到bookadd,给腾出实现上传代码的空间
2.修改servlet中bookadd的代码(添加文件上传代码)
代码如下:
//文件上传
//1,创建文件上传对象
SmartUpload smartUpload=new SmartUpload();
//2.初始化
smartUpload.initialize(super.getServletConfig(),req,resp);
//3.限制文件类型大小等等,这里我写的是图片常见的结尾
smartUpload.setAllowedFilesList("jpg,JPG,jpeg,JPEG,png,PNG,gif,GIF");
//这里是定义图片名字,数据库保存图片名字
String images="";
try {
//4,上传文件
smartUpload.upload();
System.out.println("文件上传成功!");
//5.获取文件对象
File file = smartUpload.getFiles().getFile(0);
if(file.isMissing()){
}else{
//获取upload文件夹所在的路径
String path = req.getServletContext().getRealPath("upload");
String name=path+"/"+file.getFileName();
System.out.println("路径是"+name);
//6.另存文件到upload目录
file.saveAs(name);
images=file.getFileName();//把文件名词给images,存入数据库
}
} catch (SmartUploadException e) {
System.out.println("文件上传失败!");
e.printStackTrace();
}
注意:这里是对应你要保存文件夹的名字
//获取upload文件夹所在的路径
req.getServletContext().getRealPath("upload");
3.修改servlet中bookadd的下行代码
原来的模样,用的req
修改后代码
req.setCharacterEncoding("utf-8");
Request request=smartUpload.getRequest();//上传下载组件的request对象
String bookname=request.getParameter("bookname");
String author=request.getParameter("author");
String press=request.getParameter("press");
//拿来的价格转成int类型
String pricestr=request.getParameter("price");
int price=Integer.parseInt(pricestr);
String pubtime=request.getParameter("pubtime");
//这里调用的不就是books定义的全参函数吗。。
Books books = new Books(0,bookname,author,images,press,price,pubtime);
三、显示图片
(四)、修改bug
上传的时候出现了bug,找了半天,发现form中忘记添加一句话了
五、测试
六、《修改》页面的上传代码
update中修改与add中的一样
注意这个
加一个隐藏域,将获取图片名字隐藏,上传新的名字会覆盖数据库,然后取出来文件夹对应数据库名字的图片。(之前修改的时候图片会不显示,找不到了,就是没有加隐藏于部分)。
这里的代码和添加有一点点区别
(如果没有用request获取 会出500-null 的错误)
package com.zy.servlet;
import com.jspsmart.upload.File;
import com.jspsmart.upload.Request;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import com.zy.Dao.BooksDao;
import com.zy.bean.Books;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
/**
* 类名:booksupdate
* 读书破万卷,下笔如有神
* 代码反行之,算法记于心
* 作者:劫恋李
* 日期:2021/7/8 20:57
* 版本:V1.0
*/
@WebServlet("/booksupdate")
public class booksupdate extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String images="";
//文件上传
SmartUpload smartUpload=new SmartUpload();
//初始化 initialize方法
smartUpload.initialize(super.getServletConfig(),req,resp);//getServletConfig获取配置
//限制文件类型,大小等等!!setAllowedFilesList
smartUpload.setAllowedFilesList("jpg,JPG,png,PNG,ICON,icon,jpeg,JEPG,gif,GIF");
//上传图片
try {
smartUpload.upload();
System.out.println("文件上传成功!!");
//获取文件对象
File file = smartUpload.getFiles().getFile(0);
if (file.isMissing()){
//修改 : 把原来的图片名称给images
images=smartUpload.getRequest().getParameter("images");
}else {
//获取upload文件夹所在路径!
String upload = req.getServletContext().getRealPath("upload");//
String name= upload+"/"+file.getFileName();
System.out.println("路径是什么!+"+upload);
//另存文件到upload目录
file.saveAs(name);
images=file.getFileName();//将文件名称给images,存入数据库
}
} catch (SmartUploadException e) {
System.out.println("文件上传失败!!");
e.printStackTrace();
}
req.setCharacterEncoding("utf-8");
Request request=smartUpload.getRequest();//上传下载组件的request对象
String bidStr=request.getParameter("bid");
int bid=Integer.parseInt(bidStr);
String bookname=request.getParameter("bookname");
String author=request.getParameter("author");
String press=request.getParameter("press");
//拿来的价格转成int类型
String pricestr=request.getParameter("price");
int price=Integer.parseInt(pricestr);
String pubtime=request.getParameter("pubtime");
//这里面是修改完的数据
Books books = new Books(bid,bookname,author,images,press,price,pubtime);
BooksDao booksDao = new BooksDao();
try {
//调用修改方法,修改数据
booksDao.update(books);
resp.sendRedirect("BooksAll");
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
测试
修改后
七、优化jsp的java代码
1.导入jar包
1.写c标签头文件
优化前:
优化后:
<center>
<h2><a href="booksadd.jsp">添加图书</a></h2>
<%--书名编号-图书名-作者-图片-出版社-价格-发布时间-操作--%>
<table border="1" width="80%">
<tr>
<th width="10%">书名编号</th>
<th width="10%">图书名</th>
<th width="10%">作者</th>
<th width="20%" >图片</th>
<th width="10%">出版社</th>
<th width="10%">价格</th>
<th width="20%">发布时间</th>
<th width="10%">操作</th>
</tr>
<c:forEach items="${requestScope.booksList}" var="u">
<tr>
<td>${u.bid}</td>
<td>${u.bookname}</td>
<td>${u.author}</td>
<td style="text-align: center">
<img src="upload/${u.images}" width="80px" height="60px" >
</td>
<td>${u.press}</td>
<td>${u.price}</td>
<td>${u.pubtime}</td>
<td>
<a href="BooksGoupdate?bid=${u.bid}">修改</a>
<a href="BooksDel?bid=${u.bid}" onclick="return window.confirm('是否确定删除?')">删除</a>
</td>
</tr>
</c:forEach>
</table>
</center>
没样式,主要是把jsp里面的java代码给优化一下
八、添加过滤器
1.为什么添加过滤器?
这算是个小的例子,就添加修改需要指定utf-8,如果表的数量多了就会出现问题(忘写就乱码)
2.实现过滤器
创建filter类
servle.filter (filter 不要使用错了)
web.xml 中需要配置的
<filter>
<filter-name>MyFilter</filter-name>
<filter-class>com.zy.filter.MyFilter</filter-class>
<init-param>
<param-name>encode</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>MyFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
java代码实现过滤
public class MyFilter implements Filter {
String enconding=null;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
//获取web.xml中的encond参数的值,比如utf-8
enconding=filterConfig.getInitParameter("encode");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//强转
HttpServletRequest request= (HttpServletRequest) servletRequest;
//传进来类型
request.setCharacterEncoding(this.enconding);
filterChain.doFilter(servletRequest,servletResponse);
}
@Override
public void destroy() {
}
}
可以把这句话删除了,不需要了
2.测试效果
无乱码,过滤器实现成功!
总结
本次实现了对图片的上传!还有对页面jsp的优化,加了一个过滤器
优化用到的jstl 可以搜索《EL表达式和JSTL的使用》来查看具体的使用方法。