1.要实现图片上传,首先需要一个组件,这里我用的是smartupload.jar可以到这里下载http://download.csdn.net/detail/mengdecike/8279247

2.下载之后把这个文件直接复制到WebContent/WEB-INF/lib下面

3.jsp页面

 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="UpLoad" method="post" enctype="multipart/form-data" name="form1">
<p>用户名:
<label for="username"></label>
<input type="text" name="username" id="username">
</p>
<p>头 像:
<label for="photo"></label>
<input type="file" name="photo" id="photo">
</p>
<p>
<input type="submit" name="button" id="button" value=" 提 交 ">
</p>
</form>
</body>
</html>

4.上传的servlet代码如下:

           request.setCharacterEncoding("utf-8");//设置字符
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("utf-8");
PrintWriter out =response.getWriter();//获取out输出对象 // 准备上传目录
String path = this.getServletContext().getRealPath("images");
File fpath = new File(path);
if(!fpath.exists()){
fpath.mkdir();
} // 实例化组件
SmartUpload su = new SmartUpload("utf-8");
// 初始化组件
su.initialize(this.getServletConfig(), request, response); try {
// 限定
su.setAllowedFilesList("jpg,png,gif");
su.setMaxFileSize(50*1024); // 不能超过50K // 上传并提取文件
su.upload();
SmartFile file = su.getFiles().getFile(0);
// 生成文件名
String fname = new Date().getTime()+"."+file.getFileExt();
// 保存文件
file.saveAs(path+"/"+fname);
//file.saveAs(path+"/"+fname,1);
// 提示
out.println("<script>alert('文件上传成功!');location.href='upload.jsp';</script>"); // 提取字段信息
String username = su.getRequest().getParameter("username");
System.out.println(">>> " + username); // 进行数据库操作 } catch(SecurityException e){
out.println("<script>alert('只能上传jpg、png、gif的文件并且不能超过50K!');history.back();</script>");
e.printStackTrace();
}
catch (SmartUploadException e) {
// TODO Auto-generated catch block
out.println("<script>alert('文件上传失败!');history.back();</script>");
e.printStackTrace();
}

如果需要整个的完整资源可以到http://download.csdn.net/detail/mengdecike/8279275 下载资源。

jsp图片上传的更多相关文章

  1. JSP图片上传 公共工具类

    需要jsmartcom_zh_CN.jar支持. 下载地址: http://files.cnblogs.com/simpledev/jsmartcom_zh_CN.rar <%@page imp ...

  2. [xPlugin] smartupload jsp图片上传

    URL:http://www.cnblogs.com/ISeeYouBlogs/p/jsp.html 1.要实现图片上传,首先需要一个组件,这里我用的是smartupload.jar可以到这里下载ht ...

  3. kindeditor图片上传 struts2实现

    一.kindeditor以及struts2部署搭建不再赘述,如需要请参考kindeditor使用方法 Struts2框架搭建 二.kindeditor图片上传所依赖jar包在kindeditor\js ...

  4. JSP+Servlet中使用jspsmartupload.jar进行图片上传下载

    JSP+Servlet中使用cos.jar进行图片上传 upload.jsp <form action="FileServlet" method="post&quo ...

  5. JSP+Servlet中使用cos.jar进行图片上传(文件上传亦然)

    链接:JSP+Servlet中使用jspsmartupload.jar进行图片上传下载 关于cos.jar,百度百科只有这么几句话(http://baike.baidu.com/subview/406 ...

  6. ckeditor+jsp+spring配置图片上传

    CKEditor用于富文本输入是极好的,它还有一些插件支持扩展功能,其中图片上传就是比较常用到的.本文简单记录我的实现步骤. 1.CKEditor除了提供三种标准版压缩包下载,还可根据自己的需求进行个 ...

  7. jsp+springmvc实现文件上传、图片上传和及时预览图片

    1.多文件上传:http://blog.csdn.net/a1314517love/article/details/24183273 2.单文件上传的简单示例:http://blog.csdn.net ...

  8. jsp中简易版本的图片上传程序

    1.下载相应的组件的最新版本 Commons FileUpload 可以在http://jakarta.apache.org/commons/fileupload/下载 附加的Commons IO   ...

  9. Ueditor1.4.3.3+springMvc+maven 实现图片上传

    前记:由于项目中需要有一个新增数据并且要能支持表格图片上传的功能.使用了ueditor控件.为实现这个功能,从一开始什么都看不懂,到一直连着弄了5天,总算是有了眉目.在此记录一下以便能帮到可以和我一样 ...

随机推荐

  1. Telegram学习解析系列(一):认识一下Telegram的源码

    前言: Telegram不知道有多少同行听过这玩意,或者在看它的源码.我是出于工作原因才接触到这东西,看的真是的......变方了!一个月估计刚刚找到门,还没进去多深,把自己的心得和对源码的认识以及我 ...

  2. mysql varchar类型转换int类型找出最大值

    (1) 不严谨的,最简单的 select MAX(字段名 + 0) from 表名; (2) 使用函数实现 select MAX(cast(字段名 as SIGNED INTEGER)) from 表 ...

  3. weblogic java.lang.OutOfMemoryError: PermGen space 问题解决方法

    文章转自:http://blog.csdn.net/cuihaiyang/article/details/6679735 最近安装了WebLogic10.3.4,测试在weblogic上部署项目,没过 ...

  4. ArcGIS 网络分析[1.5] 使用点线数据一起创建网络数据集(如何避免孤立点/点与线的连通性组合结果表)

    ArcGIS中最基本的三种矢量数据是什么?点线面. 网络中除了路网之外,还会有地物点. 如上图,我们在建立网络数据集的时候,作为实验,当然可以只是公路网.但是在大型的决策任务中,网络数据集就不只是公路 ...

  5. iOS开发,UITableView相关问题

    第一条:UITableViewCell 内容的设置 //文本放到最后 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_dataArr.co ...

  6. kafka的高可用和一致性探究

    一.kafka基础 本篇文章讨论的kafka版本是目前最新版 0.10.1.0. 1.1 kafka种的KafkaController 所有broker会通过ZooKeeper选举出一个作为Kafka ...

  7. bitnami gitlab 安装

    安装gitlab需要安装的依赖软件比较多,基于偷懒的原则,从网上找到了bitnami-gitlab-8.7.1-0-linux-x64-installer.run ,集成了所有的相关软件,一键安装,省 ...

  8. C语言实验单片机串口发送int型数据

    void SendIint(int n)reentrant { unsigned char s; while(n!=0) { s=(unsigned char)n%10+48; SendByte(s) ...

  9. Linux如何安装VMware Tools

    第一步:安装VMware Tools 桌面上会出现一个光盘,并且会弹出一个框框,里面是VMware Tools的安装包,将其拖到桌面上 就像下面这样 进入终端,将文件拷贝到/tmp目录下面,并解压(也 ...

  10. 走进javascript——数组的那些事

    Array构造器 如果参数只有一个并且是Number类型,那么就是指定数组的长度,但不能是NaN,如果是多个会被当做参数列表. new Array(12) // (12) [undefined × 1 ...