关于图片上传与下载(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
关于图片上传与下载(Java)的更多相关文章
- [python][flask] Flask 图片上传与下载例子(支持漂亮的拖拽上传)
目录 1.效果预览 2.新增逻辑概览 3.tuchuang.py 逻辑介绍 3.1 图片上传 3.2 图片合法检查 3.3 图片下载 4.__init__.py 逻辑介绍 5.upload.html ...
- thinkphp微信开发之jssdk图片上传并下载到本地服务器
public function test2(){ $Weixin = new \Weixin\Controller\BaseController(); $this->assign('signPa ...
- 微信JS图片上传与下载功能--微信JS系列文章(三)
概述 在前面的文章微信JS初始化-- 微信JS系列文章(一)中已经介绍了微信JS初始化的相关工作,接下来本文继续就微信JS的图片上传功能进行描述,供大家参考. 图片上传 $(function(){ v ...
- FTP文件上传和下载(JAVA)
前文 1.使用FTP的方式进行文件的上传和下载(非SFTP) 2.本人手打,亲测,代码是最简单的,清晰易懂,需要的同学请结合自己的实际添加业务逻辑 2.第三方的jar包:import org.apac ...
- 基于Angularjs实现图片上传和下载
根据ng-file-uoload实现文件上传和下载实现 网上down下来ng-file-uoload.js,在项目中记得引入服务哦. 示例代码: FileUploaderCtrl.$inject = ...
- Spring Boot +Bootstrap 图片上传与下载,以及在bootstrap-table中的显示
1.前台上传: <input type="file" name="file" id="file"> 2.后台的接收与处理: St ...
- Spring MVC 上传、下载、显示图片
目录 1. 准备工作 1.1 数据库表准备 1.2 实体类 User 和 Mapper(DAO) 1.3 pom.xml 依赖包 1.4 SSM 框架的整合配置 2. 控制器 UserControll ...
- WebApi2 文件图片上传下载
Asp.Net Framework webapi2 文件上传与下载 前端界面采用Ajax的方式执行 一.项目结构 1.App_Start配置了跨域访问,以免请求时候因跨域问题不能提交.具体的跨域配置方 ...
- 使用KindEditor完成图片上传(springmvc&fastdfs/springmvc&ftp)
前端使用KindEditor,后台使用Springmvc 1 拷贝KindEditor相关文件到项目中 拷贝KindEditor相关文件到项目中 2 准备一个jsp页面 页面中我准备了一个超链接,点击 ...
随机推荐
- laravel Lumen邮箱发送配置
Lumen 中配置邮件 https://blog.csdn.net/glovenone/article/details/54344013 Lareval 比 Lumen 多了一个步骤 https:// ...
- servlet;jsp;cookies;session
- Coursera课程《Machine Learning》学习笔记(week1)
这是Coursera上比较火的一门机器学习课程,主讲教师为Andrew Ng.在自己看神经网络的过程中也的确发现自己有基础不牢.一些基本概念没搞清楚的问题,因此想借这门课程来个查漏补缺.目前的计划是先 ...
- MySQL多实例启动停止
原文地址:http://wolfword.blog.51cto.com/4892126/1241304/ 说明:本实验以MySQL 5.1为例来实验. 1.安装MySQL 5.1 yum instal ...
- C# MD5加密与校验 引用
using System; using System.Security.Cryptography; using System.Text; class Example { // Hash an inpu ...
- Docker selinux
编辑/etc/sysconfig/docker文件,把OPTIONS='--selinux-enabled'的--selinux-enabled注释掉,也可以通过这个错误. 最大的问题就是Linux的 ...
- DIV背景图片
.bigY{ position:absolute; width:95px; height:93px; visibility:visible; right: 277 ...
- Printing tools 自定义模板打印的实现
#ArcGIS for Server 自定义打印两种方法 友好阅读版本: http://gishub.info/2013/09/17/printingtools/ ## 前言使用web打印会遇到中文乱 ...
- random生成六位随机数验证码
需求: 生成一个六位随机验证码 # 傻瓜式纯数字实现import randomran = random.randint(100000, 999999) # ran = random.randrange ...
- Scilab 的画图函数(2)
一幅图是由很多元素组成的. 包含图标题.x轴标签.y轴标签,刻度线等.图1给出了各个元素的一个示意图. 这些全部的元素在scilab中都是能够用代码控制的. 标题 上个笔记上介绍了用xtitle()函 ...