• JspSmart文件上传与下载


    JspSmart 文件上传包,放在WEB-INF/lib下

    uploadForm.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    <!DOCTYPE html>
    <html>
    <head>
        <base href="<%=basePath%>" />
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>test</title>
        <link rel="stylesheet" href="">
    </head>
    <body>
        文件上传
        <hr>
        <form action="UploadServlet" method="post" 
            enctype="multipart/form-data">
            你选择一个文件进行上传:
            <input type="file" name="myFile">
            <input type="submit" value="上传">
        </form>    
        ${msg}
    </body>
    </html>

    UploadServlet.java 

    package servlets;
    
    import java.io.IOException;
    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletConfig;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import com.jspsmart.upload.File;
    import com.jspsmart.upload.SmartUpload;
    import com.jspsmart.upload.SmartUploadException;
    
    public class UploadServlet extends HttpServlet {
        protected void doPost(HttpServletRequest request,
                HttpServletResponse response) throws ServletException, IOException {
            SmartUpload smartUpload = new SmartUpload();
            // 初始化
            ServletConfig config = this.getServletConfig();
            smartUpload.initialize(config, request, response);
            try {
                // 上传文件
                smartUpload.upload();
                // 得到上传的文件对象
                File smartFile = smartUpload.getFiles().getFile(0);
                // 保存文件
                smartFile.saveAs("C:/" + smartFile.getFileName(),
                        smartUpload.SAVE_PHYSICAL);// 保存文件到C盘根目录
            } catch (SmartUploadException e) {
                e.printStackTrace();
            }
            String msg = "Upload Success!";
            request.setAttribute("msg", msg);
            RequestDispatcher rd = request.getRequestDispatcher("/uploadForm.jsp");
            rd.forward(request, response);
        }
    }

     web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
        <display-name></display-name>
        <welcome-file-list>
            <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
        <servlet>
            <servlet-name>UploadServlet</servlet-name>
            <servlet-class>servlets.UploadServlet</servlet-class>
        </servlet>
    
    
        <servlet-mapping>
            <servlet-name>UploadServlet</servlet-name>
            <url-pattern>/UploadServlet</url-pattern>
        </servlet-mapping>
    </web-app>

    文件下载 

    download.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <%
            String filename = request.getParameter("file");
            //告诉客户端出现下载框,并指定下载框中的文件名
            response.setHeader("Content-Disposition","attachment;filename="+filename);   
            //指定文件类型   
            response.setContentType("image/jpeg");   
            //指定文件
            RequestDispatcher rd = request.getRequestDispatcher("/FILES/" + filename);
            rd.forward(request, response);
     %>

    download1.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <html>
        <body>
            文件下载
            <hr>
            <a href="download.jsp?file=img.jpg">下载</a>
        </body>
    </html>
  • 相关阅读:
    党史回顾+十九大习思想+中国历史事件+地理+航天史及古代天文历法
    国际组织
    生活-常识-物理
    生物
    数量题目总结-利润排列组合
    数量题目总结-工程最值集合
    考公错题记录表
    个人向简历介绍
    数据库操作大全
    团队博客作业-技术规格说明书
  • 原文地址:https://www.cnblogs.com/loveyunk/p/6089032.html
Copyright © 2020-2023  润新知