smartupload.jar实现文件上传,下载(附下载链接)
JavaWeb使用smartupload.jar实现文件上传,下载
1、先将smartupload.jar 导入到项目中
jar包下载地址:
点击去下载
2、上传页面的准备
注:(1)form标签中要添加enctype属性
(2)提交方式必须是post
<html>
<head><title>文件上传</title>
</head>
<body>
<form method="post" action="/upload" enctype="multipart/form-data">姓名<input name="name" type="text">年龄<input name="age" type="text">选择文件<input type="file" name="file"><input type="submit" value="上传">
</form>
</body>
</html>
3、服务器获取数据,保存文件
servlet编写
注:(1)此时如果表单中有其他数据时,不能通过request直接获取,需要通过SmartUpload对象获取
String name=su.getRequest().getParameter(“bookName”);
并且该代码要在SmartUpload操作完成后添加
(2)解决乱码:
new String(name.getBytes(“GBK”),“utf-8”)
注:斜杠方向:/
@WebServlet(urlPatterns = "/upload")
public class FileUploadServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doPost(req,resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {try{// 文件上传 -- 使用SmartUpload 对象SmartUpload su = new SmartUpload();// 获得 pageContext 对象// 使用jspFactory工厂获取JspFactory factory = JspFactory.getDefaultFactory();PageContext pageContext = factory.getPageContext(this, req, resp, null, false, 1024, true);// 初始化su.initialize(pageContext);// 设置编码su.setCharset("utf-8");// 实现文件数据上传su.upload();// 获取第一个文件File file = su.getFiles().getFile(0);// 得到文件的基本信息String filename = file.getFileName();String type = file.getContentType();System.out.println("filename="+filename);System.out.println("type="+type);String url = "uploadfile/"+filename;// 将上传文件保存到指定目录file.saveAs(url,SmartUpload.SAVE_VIRTUAL);// 设置值req.setAttribute("filename",filename);// 获取表单的其他数据项String name = su.getRequest().getParameter("name");System.out.println("username="+name);// 转发req.getRequestDispatcher("uploadSuccess.jsp").forward(req,resp);} catch (Exception e){e.printStackTrace();}}
}
获取PageContext对象时,方法 定义,参数列表如下:
smartupload常用方法:
4、上传成功的html
<html>
<head><title>上传成功</title>
</head>
<body>
<h2>上传成功<a href="/downing?filename=${filename}">下载</a></h2>
<a href="downimg?filename=${filename}">下载</a>
<img src="uploadfile/${filename}"/>
</body>
</html>
5、下载请求的servlet编写:
@WebServlet("/downing")
public class DownServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doPost(req,resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// 获取需要下载的文件名String filename = req.getParameter("filename");// 得到文件地址String url = "/uploadfile/"+filename;// 将响应的内容设置为通用的二进制流resp.setContentType("application/octet-stream");// attachment 告诉浏览器以附件的方式下载文件(弹出下载框)filename = URLEncoder.encode(filename,"utf-8");resp.addHeader("Content-Disposition","attachment;filename="+filename);// 发送req.getRequestDispatcher(url).forward(req,resp);// 清空缓存区:将服务端缓存区的文件内容,立即传送给客户端resp.flushBuffer();}
}
6、上传,下载测试 :
1、在web目录下先建立一个uploadfile文件夹(一定要先建立一个文件夹并且给他一个文件)
2、启动tomcat ,跳转到文件上传的页面,进行文件上传
3、上传成功,跳转到登录上传成功界面
4、目录:在根下有一个uploadfile文件夹
5、点击下载,文件成功下载