前期准备
jar包准备
用到两个jar包
- 1.javaee-api-7.0.jar
- 2.smartupload.jar
第一个相信大家都明白,在用到Servlet写后端的时候都会需要。 第二个则类似于与IO相关的工具类。
网页界面
这里仅仅为了实现功能,以最基础的表单为例:
提交界面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Files Download</title>
</head>
<body>
<form action="/uploadtest" method="post" enctype="multipart/form-data" >
选择图片:<input type="file" name="pic"><br>
输入:<input type="text" name="input"><br>
<input type="submit">
</form>
</body>
</html>
提交成功页
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<img src="uploadfiles/${filename}">
<a href="downimg?filename=${filename}">下载</a>
</body>
</html>
文件上传
文件上传指的是客户端向服务器上传文件,即将保存在客户端的文件上传至服务器中的一个副本,保存到服务器中。
@WebServlet(urlPatterns = "/uploadtest")
public class UpLoadServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
SmartUpload smartUpload = new SmartUpload();
PageContext pageContext = JspFactory.getDefaultFactory()
.getPageContext(this,req,resp,null,false,1024,true);
smartUpload.initialize(pageContext);
smartUpload.setCharset("utf-8");
smartUpload.upload();
File file =smartUpload.getFiles().getFile(0);
String fileName=file.getFileName();
String contentType=file.getContentType();
String text=smartUpload.getRequest().getParameter("input");
System.out.println(text);
String uploadPath = "/uploadfiles/"+fileName;
file.saveAs(uploadPath,File.SAVEAS_VIRTUAL);
req.setAttribute("filename",fileName);
req.getRequestDispatcher("success.jsp").forward(req,resp);
} catch (SmartUploadException e) {
e.printStackTrace();
}
}
}
文件下载
文件下载指的是客户端从服务器下载文件,即将原来保存在服务器中的文件下载到客户端中一个副本保存。
@WebServlet(urlPatterns = "/downimg")
public class DownLoadServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String filename=req.getParameter("filename");
String path="/uploadfiles/"+filename;
resp.setContentType("application/octet-stream");
resp.setHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode(filename,"utf-8"));
req.getRequestDispatcher(path).forward(req,resp);
resp.flushBuffer();
}
}
补充
整体结构
获取文字
此时如果表单中有其他数据时,不能通过request直接获取,需要通过SmartUpload对象获取
String name=su.getRequest().getParameter("bookName");
并且该代码要在SmartUpload操作完成后添加
解决乱码
new String(name.getBytes("GBK"),"utf-8")
getPageContext
PageContext pageContext = JspFactory.getDefaultFactory()
.getPageContext(Servlet servlet,
ServletRequest request,
ServletResponse response,
String errorPageURL,
boolean needsSession,
int buffer,
boolean autoflush);
参数 | 含义 |
---|
servlet | 请求的servlet,在servlet中传this即可 | request | servlet上挂起的当前请求 | response | servlet上挂起的当前响应 | errorPageURL | 请求JSP的错误页面的URL,或null | needsSSession | 是否需要session | buffer | 以字节为单位的缓冲区大小 | autoflush | 缓冲区应该在缓冲区溢出时自动刷新到输出流,还是抛出IOException |
smartupload常用方法
属性名称 | 说明 |
---|
public final void initialize(PageContext pageContext) | 执行上传和下载的初始化工作,必须实现 | public void upload() | 实现文件数据的上传,在 initialize方法后执行 | public int save(String pathName) | 将全部上传文件保存到指定的目录下,并返回保存的文件个数 | public void setAllowFilesList(String ExtList) | 指定允许上传的文件扩展名,接受一个扩展名列表,以逗号分割 | public void setDeniedFilesList(String fileList) | 指定了禁止上传的文件拓展名列表,每个拓展名之间以逗号分割 | public void setMaxFileSize(long filesize) | 设定每个文件允许上传的最大长度 | public void setTotalMaxFileSize(long totalfilesize) | 设定允许上传文件的总长度 |
|