jsp中如何判断上传的文件与已经存在的文件名重名,若重名如何让系统对新上传文件按一定格式自动修改文件名?

qianjinfly 2003-03-25 08:56:13
jsp中如何判断上传的文件与已经存在的文件名重名,若重名如何让系统对新上传文件按一定格式自动修改文件名?
...全文
337 3 打赏 收藏 转发到动态 举报
写回复
用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;
}

<%@ page language="java" contentType="text/html; charset=gbk"%> <%@ page pageEncoding="gbk"%> <%@ page import="com.jspsmart.upload.*" %> Insert title here
接受上传文件:okUpload.jsp 接受图片改变名称保存到指定目录并在网页上发布 接受参数值并显示在图片下面 <%@ page language="java" contentType="text/html; charset=gbk"%> <%@ page pageEncoding="gbk"%> <%@ page import="com.jspsmart.upload.*" %> <em>上传</em>处理 <% SmartUpload su=new SmartUpload(); //初始化 su.initialize(pageContext); //上传 su.upload(); // 设置保存信息 String dir="upload"; //获取上传文件列表集合 Files files=su.getFiles(); for(int i=0;i判断上传的是不是文件 if(!file.isMissing()){ //将获取的图片另存为文件为new,后缀名从原是文件获得 file.saveAs(dir+"/new."+file.getFileExt(),su.SAVE_VIRTUAL); //获取图片的客户端路径名 String name1=file.getFilePathName(); out.print("客户机原始路径名: "); out.print(name1); out.print("
"); out.print("服务器上的相对路径名: "); //构造服务器上的相对路径名 String name2=dir+"/"+"new.jpg"; out.print(name2); out.print("
"); %> <% } } %> <%=su.getRequest().getParameter("title") %>
<%=su.getRequest().getParameter("content") %>

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧