URL:http://www.cnblogs.com/ISeeYouBlogs/p/jsp.html

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

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

3.jsp页面

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
2 pageEncoding="UTF-8"%>
3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4 <html>
5 <head>
6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
7 <title>Insert title here</title>
8 </head>
9 <body>
10 <form action="UpLoad" method="post" enctype="multipart/form-data" name="form1">
11 <p>用户名:
12 <label for="username"></label>
13 <input type="text" name="username" id="username">
14 </p>
15 <p>头 像:
16 <label for="photo"></label>
17 <input type="file" name="photo" id="photo">
18 </p>
19 <p>
20 <input type="submit" name="button" id="button" value=" 提 交 ">
21 </p>
22 </form>
23 </body>
24 </html>

4.上传的servlet代码如下:

 1         
request.setCharacterEncoding("utf-8");//设置字符
2 response.setContentType("text/html;charset=utf-8");
3 response.setCharacterEncoding("utf-8");
4 PrintWriter out =response.getWriter();//获取out输出对象
5
6 // 准备上传目录
7 String path = this.getServletContext().getRealPath("images");
8 File fpath = new File(path);
9 if(!fpath.exists()){
10 fpath.mkdir();
11 }
12
13 // 实例化组件
14 SmartUpload su = new SmartUpload("utf-8");
15 // 初始化组件
16 su.initialize(this.getServletConfig(), request, response);
17
18 try {
19 // 限定
20 su.setAllowedFilesList("jpg,png,gif");
21 su.setMaxFileSize(50*1024); // 不能超过50K
22
23 // 上传并提取文件
24 su.upload();
25 SmartFile file = su.getFiles().getFile(0);
26 // 生成文件名
27 String fname = new Date().getTime()+"."+file.getFileExt();
28 // 保存文件
29 file.saveAs(path+"/"+fname);
30 //file.saveAs(path+"/"+fname,1);
31 // 提示
32 out.println("<script>alert('文件上传成功!');location.href='upload.jsp';</script>");
33
34 // 提取字段信息
35 String username = su.getRequest().getParameter("username");
36 System.out.println(">>> " + username);
37
38 // 进行数据库操作
39
40
41 } catch(SecurityException e){
42 out.println("<script>alert('只能上传jpg、png、gif的文件并且不能超过50K!');history.back();</script>");
43 e.printStackTrace();
44 }
45 catch (SmartUploadException e) {
46 // TODO Auto-generated catch block
47 out.println("<script>alert('文件上传失败!');history.back();</script>");
48 e.printStackTrace();
49 }
50

[xPlugin] smartupload jsp图片上传的更多相关文章

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

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

  2. jsp图片上传

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

  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. 酷派改变者S1(C105/C105-6/C105-8) 解锁BootLoader 并刷入recovery root

    首先下载好工具链接:https://pan.baidu.com/s/1qZjOCUw 密码:u2dr 备用下载链接:https://pan.baidu.com/s/1pMlmAef 本篇教程教你如何傻 ...

  2. DeltaFish 校园物资共享平台 第四次小组会议

    一.上周记录汇报 齐天扬 学习慕课HTML至14章.构建之法10-14章 李   鑫 学习制作简易的JSP页面和servlet,看完关于HTML的慕课 陈志锴 学习编制简易JSP页面和servlet, ...

  3. dell inspiron 15 3000 装XP win7 等GHOST系统方法

    dell inspiron 装XP win7 等GHOST系统方法 . 开机按F2,进入BIOS .在 BIOS 的Boot菜单下,将Secure Boot 改为 Disabled . 将Boot L ...

  4. (转)OL记载Arcgis Server切片

    http://blog.csdn.net/gisshixisheng/article/details/47955787 概述: 本文讲述如何在OpenLayers中调用Arcgis Server切片并 ...

  5. C# 前一个数是后一个数的父级

    private void button2_Click(object sender, EventArgs e) { var str = "1 2 3 4 5 6 7 8 9 10 11 12 ...

  6. jsp 判断当前时间是否符合设置的时间条件

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  7. [luogu 1092] 虫食算 (暴力搜索剪枝)

    传送门 Description Input 包含四行. 第一行有一个正整数 (N≤26). 后面的三行,每行有一个由大写字母组成的字符串,分别代表两个加数以及和.这3个字符串左右两端都没有空格,从高位 ...

  8. [pytorch学习]2. 官网60分钟教程摘要

    https://pytorch.org/tutorials/beginner/deep_learning_60min_blitz.html 1. Pytorch的基本单元,tensor,本质上和num ...

  9. 0214常用的SQL语句

    1MYSQL自己认为 \n 就是 换行. 如果想表达 \n 则用户必须以 \\n的方式来说明 SELECT CONCAT('"C:\\Program Files\\MySQL\\MySQL ...

  10. [bzoj4659\2694]Lcm_数论_莫比乌斯反演

    Lcm bzoj-4659 bzoj-2694 题目大意:给出A,B,考虑所有满足l<=a<=A,l<=b<=B,且不存在n>1使得n^2同时整除a和b的有序数对(a,b ...