jsp图片上传
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图片上传的更多相关文章
- JSP图片上传 公共工具类
需要jsmartcom_zh_CN.jar支持. 下载地址: http://files.cnblogs.com/simpledev/jsmartcom_zh_CN.rar <%@page imp ...
- [xPlugin] smartupload jsp图片上传
URL:http://www.cnblogs.com/ISeeYouBlogs/p/jsp.html 1.要实现图片上传,首先需要一个组件,这里我用的是smartupload.jar可以到这里下载ht ...
- kindeditor图片上传 struts2实现
一.kindeditor以及struts2部署搭建不再赘述,如需要请参考kindeditor使用方法 Struts2框架搭建 二.kindeditor图片上传所依赖jar包在kindeditor\js ...
- JSP+Servlet中使用jspsmartupload.jar进行图片上传下载
JSP+Servlet中使用cos.jar进行图片上传 upload.jsp <form action="FileServlet" method="post&quo ...
- JSP+Servlet中使用cos.jar进行图片上传(文件上传亦然)
链接:JSP+Servlet中使用jspsmartupload.jar进行图片上传下载 关于cos.jar,百度百科只有这么几句话(http://baike.baidu.com/subview/406 ...
- ckeditor+jsp+spring配置图片上传
CKEditor用于富文本输入是极好的,它还有一些插件支持扩展功能,其中图片上传就是比较常用到的.本文简单记录我的实现步骤. 1.CKEditor除了提供三种标准版压缩包下载,还可根据自己的需求进行个 ...
- jsp+springmvc实现文件上传、图片上传和及时预览图片
1.多文件上传:http://blog.csdn.net/a1314517love/article/details/24183273 2.单文件上传的简单示例:http://blog.csdn.net ...
- jsp中简易版本的图片上传程序
1.下载相应的组件的最新版本 Commons FileUpload 可以在http://jakarta.apache.org/commons/fileupload/下载 附加的Commons IO ...
- Ueditor1.4.3.3+springMvc+maven 实现图片上传
前记:由于项目中需要有一个新增数据并且要能支持表格图片上传的功能.使用了ueditor控件.为实现这个功能,从一开始什么都看不懂,到一直连着弄了5天,总算是有了眉目.在此记录一下以便能帮到可以和我一样 ...
随机推荐
- Mirantis MCP 1.0:OpenStack 和 Kubernetes 整合的第一步
1.前言 Mirantis 公司在2014年9月14日宣布收购 TCPCloud,然后宣布在2017年第一季度会推出全新的私有云产品.从那时候开始,我就一直满怀期待.终于,今年4月19日,Mirant ...
- PHP获取Mp3文件信息
扫描本地MP3文件,获取文件信息
- Jquery将<a>链接变为post请求
- Apple本地认证(密码+Touch id)
转载请注明原文链接:http://www.cnblogs.com/zhanggui/p/6839554.html 前言 本片博客主要介绍如何在自己的APP中添加指纹解锁/密码解锁技术.主要是对苹果的L ...
- VR全景加盟、720全景、VR全景技术平台-全国招商模式疯狂开始
VR全景:互联网与实体店的完美结合 VR元年已过,VR项目.VR创业潮转为理性,VR行业分为两个方向:硬件和内容.硬件又分为VR头显和辅助设备,内容又分为VR全景和VR虚拟内容,如游戏.娱乐.根据行 ...
- zoj3204 Connect them 最小生成树
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3367 题目就是简单的最小生成树的模板的应用,不过最小生成树可能不唯一 ...
- vue-router2.x
组件中的路由 <router-link to=""></router-link> 无参数 <router-link to="/ar/1&qu ...
- 学习笔记:javascript内置对象:日期对象
2.日期对象的常用函数 2.日期对象的常用函数 Date 对象方法 方法 描述 Date() 返回当日的日期和时间. getDate() 从 Date 对象返回一个月中的某一天 (1 ~ 31). ...
- PHP cURL的详细使用手册
PHP cURL的详细使用手册 PHP cURL可以帮助我们简单有效地去抓取网页内容,帮助我们方便的实现抓取功能.本文主要介绍了PHP cURL的使用方法. AD:2013云计算架构师峰会课程资料下载 ...
- [HDU1000] A + B Problem
Problem Description Calculate A + B. Input Each line will contain two integers A and B. Process to e ...