zoukankan      html  css  js  c++  java
  • 关于图片上传与下载(Java)

    图片的上传

    package com.upload;

    import java.io.IOException;
    import java.io.PrintWriter;

    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.jsp.JspFactory;
    import javax.servlet.jsp.PageContext;

    import com.jspsmart.upload.File;
    import com.jspsmart.upload.SmartUpload;
    import com.jspsmart.upload.SmartUploadException;

    @SuppressWarnings("serial")
    public class DoUploadServlet extends HttpServlet {
     public void doPost(HttpServletRequest request, HttpServletResponse response)
       throws ServletException, IOException {
      response.setContentType("text/html;charset=GB2312");
      PrintWriter out = response.getWriter();
      // 新建一个SmartUploaf对象
      SmartUpload su = new SmartUpload();
      // 得到PageContext对象
      PageContext pageContext = JspFactory
        .getDefaultFactory()
        .getPageContext(this, request, response, null, true, 8192, true);
      // 上传初始化
      su.initialize(pageContext);

      try {
       // 上传文件
       su.upload();
       // 将文件保存到指定的目录,并返回上传文件数
       int count = su.save("/js");
       out.print("<center>"+count + "个文件上传成功!<br>" + su.toString()+"</center>");
      } catch (SmartUploadException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }
      out
        .print("  <style type='text/css'>"
          + "<!--"
          + ".STYLE3 {"
          + " font-size: 12px"
          + "}"
          + "-->"
          + "</style>"
          + " <table width='600' border='1' id='tableInfo'"
          + "  style='border: 1px solid #CCCCCC; '"
          + "  cellpadding='0' cellspacing='0' align='center'>");
      // 逐一提取上传文件信息,同事保存文件
      for (int i = 0; i < su.getFiles().getCount(); i++) {
       File file = su.getFiles().getFile(i);
       // 显示当前文件信息

       out
         .print("  <tr>"
           + "   <td width='28%'>"
           + "    <div align='right' class='STYLE3'>"
           + "     表单项名(getFieldName):           </div>          </td>"
           + "   <td width='72%'>"
           + file.getFieldName()
           + "          </td>"
           + "  </tr>"
           + "  <tr>"
           + "   <td>"
           + "    <div align='right' class='STYLE3'>"
           + "     文件长度(getSize):           </div>          </td>"
           + "   <td>"
           + file.getSize()
           + "         </td>"
           + "  </tr>"
           + "  <tr>"
           + "   <td>"
           + "    <div align='right' class='STYLE3'>"
           + "     文件名(getFileName):           </div>          </td>"
           + "   <td>"
           + file.getFileName()
           + "         </td>"
           + "  </tr>"
           + "  <tr>"
           + "   <td>"
           + "    <div align='right' class='STYLE3'>"
           + "     文件扩展名(getFileExt):           </div>          </td>"
           + "   <td>"
           + file.getFileExt()
           + "          </td>"
           + "  </tr>"
           + "  <tr>"
           + "   <td>"
           + "    <div align='right' class='STYLE3'>"
           + "     文件全名(getFilePathName):           </div>          </td>"
           + "   <td>"
           + file.getFilePathName()
           + "          </td>" + "  </tr>");
      }
      out.print(" </table>");
     }
    }

    图片的下载

    package com.upload;

    import java.io.IOException;

    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.jsp.JspFactory;
    import javax.servlet.jsp.PageContext;

    import com.jspsmart.upload.SmartUpload;
    import com.jspsmart.upload.SmartUploadException;

    @SuppressWarnings("serial")
    public class DoDownLoadServlet extends HttpServlet {


     public void doGet(HttpServletRequest request, HttpServletResponse response)
       throws ServletException, IOException {

      this.doPost(request, response);
     }

     public void doPost(HttpServletRequest request, HttpServletResponse response)
       throws ServletException, IOException {

      response.setContentType("text/html;charset=GB2312");
      //得到 要下载的文件的名称
      String fileName=new String(request.getParameter("fileName").getBytes("ISO8859-1"),"GB2312");
      //得到SmartUpload对象
      SmartUpload su=new SmartUpload();
      //得到PageContext对象
      PageContext pagecontext=JspFactory.getDefaultFactory().getPageContext(this, request, response, null, true, 8192, true);
      //初始化su
      su.initialize(pagecontext);
      //下载
      
      su.setContentDisposition(null);
      try {
       su.downloadFile("/js/"+fileName);
      } catch (SmartUploadException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }
     }

    }

     相关jar包:http://www.cnblogs.com/cz-xjw/admin/Files.aspx

  • 相关阅读:
    函数声明与表达式
    js数组方法总结
    JavaScript中对象的属性
    微信小程序分辨率的问题
    排序算法---快速排序
    排序算法---归并排序
    iOS 解决1。返回时的黑边问题、2。controller的抖动问题
    UITableViewCell的separator延伸到屏幕两端
    延迟1秒
    动态计算文字的高度
  • 原文地址:https://www.cnblogs.com/cz-xjw/p/3329632.html
Copyright © 2011-2022 走看看