图片的上传

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

关于图片上传与下载(Java)的更多相关文章

  1. [python][flask] Flask 图片上传与下载例子(支持漂亮的拖拽上传)

    目录 1.效果预览 2.新增逻辑概览 3.tuchuang.py 逻辑介绍 3.1 图片上传 3.2 图片合法检查 3.3 图片下载 4.__init__.py 逻辑介绍 5.upload.html ...

  2. thinkphp微信开发之jssdk图片上传并下载到本地服务器

    public function test2(){ $Weixin = new \Weixin\Controller\BaseController(); $this->assign('signPa ...

  3. 微信JS图片上传与下载功能--微信JS系列文章(三)

    概述 在前面的文章微信JS初始化-- 微信JS系列文章(一)中已经介绍了微信JS初始化的相关工作,接下来本文继续就微信JS的图片上传功能进行描述,供大家参考. 图片上传 $(function(){ v ...

  4. FTP文件上传和下载(JAVA)

    前文 1.使用FTP的方式进行文件的上传和下载(非SFTP) 2.本人手打,亲测,代码是最简单的,清晰易懂,需要的同学请结合自己的实际添加业务逻辑 2.第三方的jar包:import org.apac ...

  5. 基于Angularjs实现图片上传和下载

    根据ng-file-uoload实现文件上传和下载实现 网上down下来ng-file-uoload.js,在项目中记得引入服务哦. 示例代码: FileUploaderCtrl.$inject = ...

  6. Spring Boot +Bootstrap 图片上传与下载,以及在bootstrap-table中的显示

    1.前台上传: <input type="file" name="file" id="file"> 2.后台的接收与处理: St ...

  7. Spring MVC 上传、下载、显示图片

    目录 1. 准备工作 1.1 数据库表准备 1.2 实体类 User 和 Mapper(DAO) 1.3 pom.xml 依赖包 1.4 SSM 框架的整合配置 2. 控制器 UserControll ...

  8. WebApi2 文件图片上传下载

    Asp.Net Framework webapi2 文件上传与下载 前端界面采用Ajax的方式执行 一.项目结构 1.App_Start配置了跨域访问,以免请求时候因跨域问题不能提交.具体的跨域配置方 ...

  9. 使用KindEditor完成图片上传(springmvc&fastdfs/springmvc&ftp)

    前端使用KindEditor,后台使用Springmvc 1 拷贝KindEditor相关文件到项目中 拷贝KindEditor相关文件到项目中 2 准备一个jsp页面 页面中我准备了一个超链接,点击 ...

随机推荐

  1. ios -完全实现代码设置 Could not find a storyboard named 'Main' in bundle NSBundle

    UIWindow *windows = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]; windows.background ...

  2. Laravel 的中大型专案架构

    好文:http://oomusou.io/laravel/laravel-architecture/

  3. 【剑指Offer面试题】 九度OJ1518:反转链表

    与其非常快写出一段漏洞百出的代码,倒不如细致分析再写出鲁棒的代码. 提前想好測试用例(输入非空等等)进行測试改动代码. 题目链接地址: http://ac.jobdu.com/problem.php? ...

  4. gcc 编译 连接 生成可执行文件

    gcc c语言编译器 g++ c++编译器 gcc a.c  生成默认的a.out 可执行文件  ./a.out  来执行 gcc -c a.c 编译生成 a.o 目标文件 可以检查语法错误 gcc ...

  5. hdu1174(3维射线与圆是否相交)

    简单的题意,要注意z2 = h2*0.9-r2 #include <iostream> #include <cmath> #include <vector> #in ...

  6. Exponentiation(高精度大数)

    Exponentiation Description Problems involving the computation of exact values of very large magnitud ...

  7. Qt里的原子操作QAtomicInteger

    所谓原子操作,即一系列复杂的操作能一气呵成,中间不被其他的操作打断.这在多线程程序中尤其常见,但要实现这种功能,既要考虑程序的良好设计,又要关心特定平台的体系结构和相关编译器对原子特性的支持程度.所以 ...

  8. Vue父组件调用子组件的方法

    vue中如果父组件想调用子组件的方法,可以在子组件中加上ref,然后通过this.$refs.ref.method调用,例如: 父组件: <template> <div @click ...

  9. postgrSQL 错误ERROR: permission denied

    赋权限: GRANT ALL PRIVILEGES ON TABLE 表名 TO 用户;

  10. Meteor工作目录的划分

    现在说明一下Meteor的工作目录是这样划分的,但是在说明之前 做个约定,以免后面造成混淆或错误.  我们通过 meteor create API-002-Core创建meteor工程后,那么就会有一 ...