smartupload.jar(使用smartupload.jar实现文件上传下载)

发布时间:2025-12-10 19:23:14 浏览次数:4

使用smartupload.jar实现文件上传下载-

使用smartupload.jar实现文件上传下载使用smartupload.jar实现文件上传下载准备上传的页面<%@pagecontentType=”text/html;charset=UTF-8″language=”java”%><html><head><title>文件上传下载</title></head><body><formaction=”/uploadtest”method=”post”enctype=”multip

使用smartupload.jar实现文件上传下载

准备上传的页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head>    <title>文件上传下载</title></head><body><form action="/uploadtest" method="post" enctype="multipart/form-data">    图片:<input type="file" name="pic">    <input type="submit" value="上传"></form></body></html>

是否还在为Ide开发工具频繁失效而烦恼,来吧关注以下公众号获取最新激活方式。亲测可用!

为防止网络爬虫,请关注公众号回复”口令”

激活idea 激活CLion DataGrip DataSpell dotCover dotMemory dotTrace GoLand PhpStorm PyCharm ReSharper ReShaC++ Rider RubyMine WebStorm 全家桶 刷新

【正版授权,激活自己账号】:Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】:官方授权 正版激活 自己使用,支持Jetbrains家族下所有IDE…

注:(1)form标签中要添加enctype属性
(2)提交方式必须是post

开始获取数据,保存文件

@Override    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {            //1.创建上传文件的对象        SmartUpload smartUpload = new SmartUpload();        //2.初始化上传操作        PageContext pageContext = JspFactory.getDefaultFactory().getPageContext(this, req, resp, null, false, 1024, true);        smartUpload.initialize(pageContext);        //2.1 设置编码        smartUpload.setCharset("utf-8");        //3.上传        try {                smartUpload.upload();        } catch (SmartUploadException e) {                e.printStackTrace();        }        //4.获取文件信息        File file = smartUpload.getFiles().getFile(0);        String fileName = file.getFileName();        String contentType = file.getContentType();        //5.指定上传的路径        String uploadPath = "/uploadfiles/"+fileName;        //6.保存到指定位置        try {                file.saveAs(uploadPath,File.SAVEAS_VIRTUAL);        } catch (SmartUploadException e) {                e.printStackTrace();        }        //7.跳转到指定页面        req.setAttribute("fileName",fileName);        req.getRequestDispatcher("show.jsp").forward(req,resp);    }

注:
(1)此时如果表单中有其他数据时,不能通过request直接获取,需要通过SmartUpload对象获取 String name=su.getRequest().getParameter(“bookName”);并且该代码要在SmartUpload操作完成后添加
(2)解决乱码:
new String(name.getBytes(“GBK”),“utf-8”)

注:斜杠方向:/

注意:


smartupload常用方法

文件下载

    @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.addHeader("Content-Disposition","attachment;filename="+filename);        //跳转页面        req.getRequestDispatcher(path).forward(req,resp);        //清空缓存区        resp.flushBuffer();    }

效果如下:

上传之后跳到show页面

--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head>    <title>文件上传展示页面</title></head><body><a href="${pageContext.request.contextPath}/download?filename=${fileName}">下载</a><img src="uploadfiles/${fileName}"/></body></html>

点击下载之后,存入对应文件夹

需要做网站?需要网络推广?欢迎咨询客户经理 13272073477