smartupload.jar实现文件上传,下载(附下载链接)[亲测有效]

(46) 2023-06-15 14:12

Hi,大家好,我是编程小6,很荣幸遇见你,我把这些年在开发过程中遇到的问题或想法写出来,今天说一说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 { 
   
    @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 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.jar实现文件上传,下载(附下载链接)[亲测有效]_https://bianchenghao6.com/blog__第1张

smartupload常用方法:

smartupload.jar实现文件上传,下载(附下载链接)[亲测有效]_https://bianchenghao6.com/blog__第2张

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 { 
   
    @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 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文件夹(一定要先建立一个文件夹并且给他一个文件)

smartupload.jar实现文件上传,下载(附下载链接)[亲测有效]_https://bianchenghao6.com/blog__第3张

2、启动tomcat ,跳转到文件上传的页面,进行文件上传

smartupload.jar实现文件上传,下载(附下载链接)[亲测有效]_https://bianchenghao6.com/blog__第4张

3、上传成功,跳转到登录上传成功界面

smartupload.jar实现文件上传,下载(附下载链接)[亲测有效]_https://bianchenghao6.com/blog__第5张

4、目录:在根下有一个uploadfile文件夹

smartupload.jar实现文件上传,下载(附下载链接)[亲测有效]_https://bianchenghao6.com/blog__第6张

5、点击下载,文件成功下载

smartupload.jar实现文件上传,下载(附下载链接)[亲测有效]_https://bianchenghao6.com/blog__第7张

上一篇

已是最后文章

下一篇

已是最新文章

发表回复