关于图片上传与下载(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

转载于:https://www.cnblogs.com/cz-xjw/p/3329632.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值