社区
Web 开发
帖子详情
jsp中如何判断上传的文件与已经存在的文件名重名,若重名如何让系统对新上传文件按一定格式自动修改文件名?
qianjinfly
2003-03-25 08:56:13
jsp中如何判断上传的文件与已经存在的文件名重名,若重名如何让系统对新上传文件按一定格式自动修改文件名?
...全文
355
3
打赏
收藏
jsp中如何判断上传的文件与已经存在的文件名重名,若重名如何让系统对新上传文件按一定格式自动修改文件名?
jsp中如何判断上传的文件与已经存在的文件名重名,若重名如何让系统对新上传文件按一定格式自动修改文件名?
复制链接
扫一扫
分享
转发到动态
举报
AI
作业
写回复
配置赞助广告
用AI写文章
3 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
qiri07
2003-03-25
打赏
举报
回复
gz,study
yuebenxian
2003-03-25
打赏
举报
回复
/***********************************************
功能: 匹配函数
使用说明:
参数含义: aTarget:要匹配的字符串
aPattern:匹配模式
返回值含义:如果匹配成功,返回true;反之,返回false
************************************************/
private boolean match(String aTarget, String aPattern)
{
String target = aTarget;
String pattern = aPattern;
if(aTarget == null)
throw new IllegalArgumentException("aTarget is null");
if(aPattern == null)
throw new IllegalArgumentException("aPattern is null");
if(pattern.equals("*.*"))
pattern = "*";
if(pattern.equals(arbString))
return true;
if(target.equals(pattern))
return true;
if(patExtEmpty && target.length() == 1 && tagExtEmpty && pattern.equals("."))
{
patExtEmpty = false;
tagExtEmpty = false;
return true;
}
if(target.equals(emptyString)) //要匹配的串为空
return false;
if(pattern.equals(emptyString)) //匹配模式为空
return false;
//arbString 初始值为"*" arbChar初始值为"?"
if(pattern.substring(0, 1).equals(arbString) && pattern.substring(1, 2).equals(arbString))
pattern = pattern.substring(1);
else if(pattern.substring(0, 1).equals(arbString) && pattern.substring(1, 2).equals(arbChar))
{
pattern = arbChar + arbString + pattern.substring(2);
}
else
{
if(pattern.substring(0, 1).equals(arbChar)) //如果匹配模式的第一个字符为\u00B4?\u00B4,则从第二个字符开始比较
return match(target.substring(1), pattern.substring(1));
if(pattern.substring(0, 1).equals(arbString))
{
for(int i = 0; i < target.length(); i++)
if(match(target.substring(i), pattern.substring(1)))
return true;
return false;
}
if(pattern.substring(0, 1).equals(target.substring(0, 1)))
return match(target.substring(1), pattern.substring(1));
else
return false;
}
return match(target, pattern);
}
/***********************************************
功能: 搜索指定路径的文件。
使用说明:
参数含义: path:指定要搜索的路径
返回值含义:无
************************************************/
private void searchFolder(String path)
{
File f = new File(path);
String list[] = f.list(); //返回由path指定路径里的所有文件名和目录名
if(list != null){
for(int i = 0; i < list.length; i++)
{
File file = new File(path + list[i]);
if(file.isFile())
{
if(list[i].indexOf(".") == -1) //文件没有后缀
tagExtEmpty = true;
else
tagExtEmpty = false;
if(fieldInputPattern.charAt(fieldInputPattern.length() - 1) == '.')
patExtEmpty = true;
if(match(list[i], fieldInputPattern))
{
String resultPath = file.getPath();
int _tmp = fieldResultCountOfFoundFiles;
searchResult.addElement(resultPath);
String oldResultLastFoundFilename = fieldResultLastFoundFilename;
fieldResultLastFoundFilename = resultPath;
fieldResultCountOfFoundFiles = searchResult.size();
}
} else
if(fieldRecurse)
{
searchFolder(path + list[i] + System.getProperty("file.separator"));
}
}
}
}
public void setIllegalSymbols(String illegalSymbols)
{
String oldValue = fieldIllegalSimbols;
fieldIllegalSimbols = illegalSymbols;
}
public void setInputPath(String inputPath)
{
if(inputPath == null)
throw new IllegalArgumentException("Can not set inputPath to null!");
if(!inputPath.endsWith(System.getProperty("file.separator")) && !inputPath.endsWith("/") && inputPath.length() > 0)
inputPath = inputPath + System.getProperty("file.separator");
fieldInputPath = inputPath;
}
/***********************************************
功能: 设置匹配模式
使用说明:
参数含义: inputPattern:匹配模式
返回值含义:无
************************************************/
public void setInputPattern(String inputPattern)
{
if(inputPattern == null || inputPattern.length() == 0)
return;
if(inputPattern == ".")
return;
fieldInputPattern = inputPattern;
}
public void setMultipleCharacterPattern(String multipleCharacterPattern)
{
String _tmp = fieldMultipleCharacterPattern;
fieldMultipleCharacterPattern = multipleCharacterPattern;
if(multipleCharacterPattern == null || multipleCharacterPattern.length() == 0)
throw new IllegalArgumentException("Can not set multipleCharacterPattern to null!");
fieldMultipleCharacterPattern = multipleCharacterPattern;
if(multipleCharacterPattern.length() == 1)
{
arbString = multipleCharacterPattern;
} else
{
arbString = multipleCharacterPattern.substring(0, 1);
fieldMultipleCharacterPattern = arbString;
}
}
/***********************************************
功能: 是否搜索子文件夹
使用说明:
参数含义: recurse:true ,搜索
false,不搜索
返回值含义:无
************************************************/
public void setRecurse(boolean recurse)
{
boolean oldValue = fieldRecurse;
fieldRecurse = recurse;
}
public void setSingleCharacterPattern(String singleCharacterPattern)
{
String _tmp = fieldSingleCharacterPattern;
if(singleCharacterPattern == null || singleCharacterPattern.length() == 0)
throw new IllegalArgumentException("Can not set singleCharacterPattern to null!");
if(singleCharacterPattern.length() == 1)
fieldSingleCharacterPattern = singleCharacterPattern;
else
fieldSingleCharacterPattern = singleCharacterPattern.substring(0, 1);
arbChar = fieldSingleCharacterPattern;
}
/***********************************************
功能: 搜索文件
使用说明:
参数含义: 无
返回值含义:结果集
-1:搜索路径不存在或不是个目录
-2:搜索失败
1:搜索成功
************************************************/
public Vector findFile(){
if(computeFunction()==1)
return searchResult;
else{
searchResult.clear();
if(computeFunction()==-1){
searchResult.addElement("-1");
}else{
searchResult.addElement("-2");
}
}
return searchResult;
}
public findFile()
{
oldSearchResult = new Vector();
searchResult = new Vector(10);
operations = new Vector();
strings = new Vector();
arbString = "*";
arbChar = "?";
emptyString = "";
fieldIllegalSimbols = "<>|";
fieldMultipleCharacterPattern = "*";
fieldSingleCharacterPattern = "?";
fieldRecurse = false;
fieldResultLastFoundFilename = "";
fieldInputPath = "";
fieldInputPattern = "";
fieldResultListOfFilenames = new String[0];
patExtEmpty = false;
tagExtEmpty = false;
fieldResultCountOfFoundFiles = 0;
abort = false;
}
}
yuebenxian
2003-03-25
打赏
举报
回复
1.rename()可以改名...
2/ 判断是否.....
package LWClass;
import java.io.*;
import java.util.Vector;
import java.awt.List;
/**********************************************
功能说明:
★ 根据指定的路径,查找文件
***********************************************/
public class findFile
{
private Vector oldSearchResult;
private Vector searchResult;
private Vector operations;
private Vector strings;
private String arbString;
private String arbChar;
String emptyString;
String fieldIllegalSimbols;
String fieldMultipleCharacterPattern;
String fieldSingleCharacterPattern;
boolean fieldRecurse;
String fieldResultLastFoundFilename;
String fieldInputPath;
String fieldInputPattern;
String fieldResultListOfFilenames[];
boolean patExtEmpty;
boolean tagExtEmpty;
int fieldResultCountOfFoundFiles;
private boolean abort;
protected String[] result;
public void abort()
{
abort = true;
}
/***********************************************
功能: 查找文件
使用说明:
参数含义: 无
返回值含义:-1:搜索路径不存在或不是个目录
-2:搜索失败
1:搜索成功
************************************************/
protected int computeFunction()
{
oldSearchResult = (Vector)searchResult.clone();
searchResult.removeAllElements();
if(fieldMultipleCharacterPattern.equals(""))
return -2;
if(fieldSingleCharacterPattern.equals(""))
return -2;
for(int i = 0; i < fieldIllegalSimbols.length(); i++)
if(fieldInputPath.indexOf(fieldIllegalSimbols.charAt(i)) != -1)
return -2;
File path = new File(fieldInputPath);
if(!path.exists() || !path.isDirectory()) //如果搜索路径不存在或不是个目录
return -1;
searchFolder(fieldInputPath);
if(!searchResult.isEmpty())
{
String arr[] = new String[searchResult.size()];
searchResult.copyInto(arr);
fieldResultListOfFilenames = new String[arr.length];
System.arraycopy(arr, 0, fieldResultListOfFilenames, 0, arr.length);
fieldResultCountOfFoundFiles = searchResult.size();
}
return 1;
}
public String getIllegalSymbols()
{
if(fieldIllegalSimbols == null)
try
{
fieldIllegalSimbols = new String();
}
catch(Throwable _ex)
{
System.err.println("Exception creating illegalSimbolsproperty.");
}
return fieldIllegalSimbols;
}
public String getInputPath()
{
if(fieldInputPath == null)
try
{
fieldInputPath = new String();
}
catch(Throwable _ex)
{
System.err.println("Exception creating inputPathproperty.");
}
return fieldInputPath;
}
public String getInputPattern()
{
if(fieldInputPattern == null)
try
{
fieldInputPattern = new String();
}
catch(Throwable _ex)
{
System.err.println("Exception creating inputPatternproperty.");
}
return fieldInputPattern;
}
public String getMultipleCharacterPattern()
{
if(fieldMultipleCharacterPattern == null)
try
{
fieldMultipleCharacterPattern = new String();
}
catch(Throwable _ex)
{
System.err.println("Exception creating multipleCharacterPatternproperty.");
}
return fieldMultipleCharacterPattern;
}
public boolean getRecurse()
{
return fieldRecurse;
}
public int getResultCountOfFoundFiles()
{
fieldResultCountOfFoundFiles = searchResult.size();
return fieldResultCountOfFoundFiles;
}
public String getResultLastFoundFilename()
{
if(fieldResultLastFoundFilename == null)
try
{
fieldResultLastFoundFilename = new String();
}
catch(Throwable _ex)
{
System.err.println("Exception creating resultLastFoundFilenameproperty.");
}
return fieldResultLastFoundFilename;
}
public String[] getResultListOfFilenames()
{
return fieldResultListOfFilenames;
}
public String getSingleCharacterPattern()
{
if(fieldSingleCharacterPattern == null)
try
{
fieldSingleCharacterPattern = new String();
}
catch(Throwable _ex)
{
System.err.println("Exception creating singleCharacterPatternproperty.");
}
return fieldSingleCharacterPattern;
}
Jsp
SmartUpload
上传
文件
到
文件
夹
重名
问题解决方法
//
判断
上传
的是不是
文件
if(!file.isMissing()){ //将获取的图片另存为
文件
名
为new,后缀名从原是
文件
中
获得 file.saveAs(dir+"/new."+file.getFileExt(),su.SAVE_VIRTUAL); //获取图片的客户端路径名 String ...
利用
jsp
SmartUpload组件进行
文件
上传
在Java Web开发
中
,
文件
上传
是一项常见的功能,用于让用户
上传
图片、文档等资源到服务器。
JSP
Smart Upload组件是一款强大的
文件
上传
工具,尤其适合在
JSP
环境下使用。它提供了便捷的API,使得开发者能够轻松地处理...
JSP
多附件
上传
系统
【
JSP
多附件
上传
系统】是一个基于Java
JSP
技术实现的Web应用,旨在提供一个能够支持用户
上传
多个
文件
的功能。在传统的Web开发
中
,单个
文件
上传
是常见的操作,但随着互联网应用的发展,多
文件
上传
的需求越来越普遍,...
JSP
实现
文件
上传
(java的io流实现)
同时,由于
JSP
直接处理
文件
上传
可能不太安全,通常会推荐将
文件
上传
逻辑放在单独的Servlet
中
,以更好地控制和管理
上传
过程。 在处理
文件
上传
时,还需要考虑一些最佳实践,例如: 1. 检查
文件
类型和大小,防止恶意...
文件
上传
jsp
,servlet编写
在Java Web开发
中
,
JSP
(JavaServer Pages)和Servlet是两种常见的技术,它们用于创建动态、交互式的网页。...开发者需要理解
JSP
与Servlet之间的协作机制,掌握
文件
上传
的相关技术和安全措施,才能构建出这样一个系统。
Web 开发
81,122
社区成员
341,744
社区内容
发帖
与我相关
我的任务
Web 开发
Java Web 开发
复制链接
扫一扫
分享
社区描述
Java Web 开发
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章