关于JSMARK上传图片的学习东东
2007-08-17 10:42
344 查看
记得下载jar包啊 别忘了把名改成smartUpload.jar
UploadFiles/2006-8/816529534.rar
Utility类:
package com.tidyinfo.utils;
import java.util.Calendar;
public class Utility {
//生成形如:/2006/08/18的路径
private String path;
//生成形如: 20060818125012的唯一id号
private String id;
//生成形如:/2006/08/18的路径
private String url;
private String hour;
private String year;
private String month;
private String day;
private String minate;
private String second;
/** Creates a new instance of Utility */
public Utility() {
this.year =new Integer(Calendar.getInstance().get(Calendar.YEAR)).toString();
this.month = parseToString(Calendar.getInstance().get(Calendar.MONTH)+1);
this.day = parseToString(Calendar.getInstance().get(Calendar.DAY_OF_MONTH));
this.hour = parseToString(Calendar.getInstance().get(Calendar.HOUR_OF_DAY));
this.minate = parseToString(Calendar.getInstance().get(Calendar.MINUTE));
this.second = parseToString(Calendar.getInstance().get(Calendar.SECOND));
this.setId();
this.setPath();
this.setUrl();
}
protected String parseToString(int i){
return i<10?("0"+i):new Integer(i).toString();
}
public String getId(){
return this.id;
}
public String getPath(){
return this.path;
}
public String getUrl(){
return this.url;
}
protected void setId(){
this.id = this.year+this.month+this.day+this.hour+this.minate+this.second;
}
protected void setPath(){
this.path = "//"+this.year + "//" + this.month + "//" +
this.day ;
}
protected void setUrl(){
this.url = this.path.replace('//','/');
}
}
Action:(根据不同情况要更改相关代码)
User userForm = new User();
String result = new String();
SmartUpload su = new SmartUpload();
//文件名
String filename = new String();
//文件扩展名
String fileext = new String();
//文件上传后存储的路径
String path = request.getRealPath(request.getContextPath());
path = path.substring(0,path.lastIndexOf("//"));
path += "//images";
//图片的url
String url = new String();
//创建文件夹
java.io.File file = new java.io.File(path);
file.mkdirs();
try{
// 上传初始化
su.initialize(this.servlet.getServletConfig(),request,response);
//设定上传限制
//1.限制每个上传照片的最大长度。
su.setMaxFileSize(5000000);
//2.限制总上传数据的长度。
su.setTotalMaxFileSize(10000000);
//3.设定允许上传的照片(通过扩展名限制)。
su.setAllowedFilesList("jpg,gif,GIF,JPG");
//上传照片
su.upload();
//获得请求的表单数据
String username = su.getRequest().getParameter("username");//username
String password = su.getRequest().getParameter("password");//password
String sex = su.getRequest().getParameter("sex");//sex
String email =su.getRequest().getParameter("email");//email
String apwd =su.getRequest().getParameter("apwd");//question of password
String rpwd =su.getRequest().getParameter("rpwd");//anser of password
userForm.setUsername(username);
userForm.setPassword(password);
userForm.setSex(sex);
userForm.setEmail(email);
userForm.setApwd(apwd);
userForm.setRpwd(rpwd);
//将上传照片全部保存到指定目录
if(!su.getFiles().getFile(0).isMissing()){
su.save(path,su.SAVE_PHYSICAL);
//文件名
filename = su.getFiles().getFile(0).getFileName();
//得到扩展名
fileext = su.getFiles().getFile(0).getFileExt();
//给图片改名,命名形如:20060814135411.gif
Utility u = new Utility();
String newName = u.getId()+"."+fileext;
java.io.File oldFile = new java.io.File(path+"//"+filename);
java.io.File newFile = new java.io.File( path + "//"+newName);
//上传图片的url
url = request.getContextPath()+"/images/"+newName;
if(oldFile!=null){
oldFile.renameTo(newFile);
}else{
result+="命名出错!";
}
System.out.println("0000000000000000000"+filename);
System.out.println("0000000000000000000"+path+"//"+newName);
System.out.println("0000000000000000000"+url);
System.out.println(result);
}
}catch(com.jspsmart.upload.SmartUploadException e){
result += "file出错信息:";
result += e.getMessage();
System.out.println(result);
}
--------------------------------------------------
SmartUpload在servlet中使用方法
SmartUpload mySmartUpload = new SmartUpload();
mySmartUpload.initialize(servlet.getServletConfig(), request,re
4000
sponse);
// 设定上传限制
// 1.限制每个上传文件的最大长度。
// mySmartUpload.setMaxFileSize(10000);
// 2.限制总上传数据的长度。
// mySmartUpload.setTotalMaxFileSize(20000);
// 3.设定允许上传的文件(通过扩展名限制),仅允许doc,txt文件。
// mySmartUpload.setAllowedFilesList("doc,txt");
// 4.设定禁止上传的文件(通过扩展名限制),禁止上传带有exe,bat, jsp,htm,html扩展名的文件和没有扩展名的文件。
// mySmartUpload.setDeniedFilesList("exe,bat,jsp,htm,html,,");
mySmartUpload.upload();
//读取其它数据
Request req = mySmartUpload.getRequest();
String title = req.getParameter("title");
//保存文件
for (int i = 0; i < mySmartUpload.getFiles().getCount(); i++) {
com.jspsmart.upload.File file = mySmartUpload.getFiles().getFile(i);
if (file.isMissing()) continue;
file.saveAs(savePath + file.getFileName());
}
UploadFiles/2006-8/816529534.rar
Utility类:
package com.tidyinfo.utils;
import java.util.Calendar;
public class Utility {
//生成形如:/2006/08/18的路径
private String path;
//生成形如: 20060818125012的唯一id号
private String id;
//生成形如:/2006/08/18的路径
private String url;
private String hour;
private String year;
private String month;
private String day;
private String minate;
private String second;
/** Creates a new instance of Utility */
public Utility() {
this.year =new Integer(Calendar.getInstance().get(Calendar.YEAR)).toString();
this.month = parseToString(Calendar.getInstance().get(Calendar.MONTH)+1);
this.day = parseToString(Calendar.getInstance().get(Calendar.DAY_OF_MONTH));
this.hour = parseToString(Calendar.getInstance().get(Calendar.HOUR_OF_DAY));
this.minate = parseToString(Calendar.getInstance().get(Calendar.MINUTE));
this.second = parseToString(Calendar.getInstance().get(Calendar.SECOND));
this.setId();
this.setPath();
this.setUrl();
}
protected String parseToString(int i){
return i<10?("0"+i):new Integer(i).toString();
}
public String getId(){
return this.id;
}
public String getPath(){
return this.path;
}
public String getUrl(){
return this.url;
}
protected void setId(){
this.id = this.year+this.month+this.day+this.hour+this.minate+this.second;
}
protected void setPath(){
this.path = "//"+this.year + "//" + this.month + "//" +
this.day ;
}
protected void setUrl(){
this.url = this.path.replace('//','/');
}
}
Action:(根据不同情况要更改相关代码)
User userForm = new User();
String result = new String();
SmartUpload su = new SmartUpload();
//文件名
String filename = new String();
//文件扩展名
String fileext = new String();
//文件上传后存储的路径
String path = request.getRealPath(request.getContextPath());
path = path.substring(0,path.lastIndexOf("//"));
path += "//images";
//图片的url
String url = new String();
//创建文件夹
java.io.File file = new java.io.File(path);
file.mkdirs();
try{
// 上传初始化
su.initialize(this.servlet.getServletConfig(),request,response);
//设定上传限制
//1.限制每个上传照片的最大长度。
su.setMaxFileSize(5000000);
//2.限制总上传数据的长度。
su.setTotalMaxFileSize(10000000);
//3.设定允许上传的照片(通过扩展名限制)。
su.setAllowedFilesList("jpg,gif,GIF,JPG");
//上传照片
su.upload();
//获得请求的表单数据
String username = su.getRequest().getParameter("username");//username
String password = su.getRequest().getParameter("password");//password
String sex = su.getRequest().getParameter("sex");//sex
String email =su.getRequest().getParameter("email");//email
String apwd =su.getRequest().getParameter("apwd");//question of password
String rpwd =su.getRequest().getParameter("rpwd");//anser of password
userForm.setUsername(username);
userForm.setPassword(password);
userForm.setSex(sex);
userForm.setEmail(email);
userForm.setApwd(apwd);
userForm.setRpwd(rpwd);
//将上传照片全部保存到指定目录
if(!su.getFiles().getFile(0).isMissing()){
su.save(path,su.SAVE_PHYSICAL);
//文件名
filename = su.getFiles().getFile(0).getFileName();
//得到扩展名
fileext = su.getFiles().getFile(0).getFileExt();
//给图片改名,命名形如:20060814135411.gif
Utility u = new Utility();
String newName = u.getId()+"."+fileext;
java.io.File oldFile = new java.io.File(path+"//"+filename);
java.io.File newFile = new java.io.File( path + "//"+newName);
//上传图片的url
url = request.getContextPath()+"/images/"+newName;
if(oldFile!=null){
oldFile.renameTo(newFile);
}else{
result+="命名出错!";
}
System.out.println("0000000000000000000"+filename);
System.out.println("0000000000000000000"+path+"//"+newName);
System.out.println("0000000000000000000"+url);
System.out.println(result);
}
}catch(com.jspsmart.upload.SmartUploadException e){
result += "file出错信息:";
result += e.getMessage();
System.out.println(result);
}
--------------------------------------------------
SmartUpload在servlet中使用方法
SmartUpload mySmartUpload = new SmartUpload();
mySmartUpload.initialize(servlet.getServletConfig(), request,re
4000
sponse);
// 设定上传限制
// 1.限制每个上传文件的最大长度。
// mySmartUpload.setMaxFileSize(10000);
// 2.限制总上传数据的长度。
// mySmartUpload.setTotalMaxFileSize(20000);
// 3.设定允许上传的文件(通过扩展名限制),仅允许doc,txt文件。
// mySmartUpload.setAllowedFilesList("doc,txt");
// 4.设定禁止上传的文件(通过扩展名限制),禁止上传带有exe,bat, jsp,htm,html扩展名的文件和没有扩展名的文件。
// mySmartUpload.setDeniedFilesList("exe,bat,jsp,htm,html,,");
mySmartUpload.upload();
//读取其它数据
Request req = mySmartUpload.getRequest();
String title = req.getParameter("title");
//保存文件
for (int i = 0; i < mySmartUpload.getFiles().getCount(); i++) {
com.jspsmart.upload.File file = mySmartUpload.getFiles().getFile(i);
if (file.isMissing()) continue;
file.saveAs(savePath + file.getFileName());
}
相关文章推荐
- 关于ajaxfileupload.js上传图片使用历程(struts2)
- node.js学习-在页面上传图片并显示例子
- 关于图片上传限制的js
- 从零开始学习Node.js例子三 图片上传和显示
- 从零开始学习Node.js系列教程三:图片上传和显示方法示例
- sql server 关于表中只增标识问题 C# 实现自动化打开和关闭可执行文件(或 关闭停止与系统交互的可执行文件) ajaxfileupload插件上传图片功能,用MVC和aspx做后台各写了一个案例 将小写阿拉伯数字转换成大写的汉字, C# WinForm 中英文实现, 国际化实现的简单方法 ASP.NET Core 2 学习笔记(六)ASP.NET Core 2 学习笔记(三)
- 关于大型信息系统架构中js、css、images及用户上传图片、流媒体等等资源部署规则
- JS学习32:html5拖拽图片批量ajax无刷新进度上传
- 关于ajaxfileupload.js一些问题和上传图片就立即显示图片功能
- IPFS + 区块链 系列】 入门篇 - IPFS + Ethereum (中篇)-js-ipfs-api - 图片上传到IPFS以及下载
- jquery.uploadView.js图片上传插件
- js解决上传图片的预览
- js 实现异步上传图片+预览
- 上传图片---SpringMVC学习笔记(十一)
- js验证上传图片文件大小,类型等
- 关于IE6和IE7关闭窗口时提示和不提示以及上传图片前的本地预览解决办法
- nodeJs学习过程之一个图片上传显示的例子
- 调用微信JS-SDK上传图片爬坑之路
- JS 图片压缩上传并在iOS中矫正方向
- js上传图片并预览