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

使用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>

注:(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>

在这里插入图片描述

点击下载之后,存入对应文件夹
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值