在用jspsmart上传文件的问题

lxglxg84112 2003-04-06 11:06:46
我在用jspsmart上传文件的时候,把文件存在名为upload的文件夹中,但是如果
上传的文件名字是相同的话,就会把前一文件覆盖呀,怎么改变上传文件的名字呀
我是么具体的程序,谢谢高手来帮着解决!!!
...全文
97 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
meizizi 2003-04-06
  • 打赏
  • 举报
回复
或者
try {
FileInputStream inStream = new FileInputStream(yourFile);//包括路径名。
或类似的方法都可以。
}catch(FileNotFoundException e) {
在这里做你的文件名修改,不和原来的文件同名。
再继续。
}
meizizi 2003-04-06
  • 打赏
  • 举报
回复
或者
try {
FileInputStream inStream = new FileInputStream(fileDestination);
}catch(FileNotFoundException e) {

}
meizizi 2003-04-06
  • 打赏
  • 举报
回复
重载upload() 方法

public void upload(String newFileName)
throws SmartUploadException, IOException, ServletException
{
int totalRead = 0;
int readBytes = 0;
long totalFileSize = 0L;
boolean found = false;
String dataHeader = new String();
String fieldName = new String();
String fileName = new String();
String fileExt = new String();
String filePathName = new String();
String contentType = new String();
String contentDisp = new String();
String typeMIME = new String();
String subTypeMIME = new String();
boolean isFile = false;
m_totalBytes = m_request.getContentLength();
m_binArray = new byte[m_totalBytes];
for(; totalRead < m_totalBytes; totalRead += readBytes)
//System.out.println("to talRead begin !");
try
{
m_request.getInputStream();
System.out.println("to talRead 11111111 !");
readBytes = m_request.getInputStream().read(m_binArray, totalRead, m_totalBytes - totalRead);
System.out.println("to talRead end readBytes !"+readBytes);
}
catch(Exception e)
{
throw new SmartUploadException("Unable to upload.");
}

for(; !found && m_currentIndex < m_totalBytes; m_currentIndex++)
if(m_binArray[m_currentIndex] == 13)
found = true;
else
m_boundary = m_boundary + (char)m_binArray[m_currentIndex];

if(m_currentIndex == 1)
return;
m_currentIndex++;
do
{
if(m_currentIndex >= m_totalBytes)
break;
dataHeader = getDataHeader();
m_currentIndex = m_currentIndex + 2;
isFile = dataHeader.indexOf("filename") > 0;
fieldName = getDataFieldValue(dataHeader, "name");
if(isFile)
{
filePathName = getDataFieldValue(dataHeader, "filename");
fileName = getFileName(filePathName);
fileExt = getFileExt(fileName);
contentType = getContentType(dataHeader);
contentDisp = getContentDisp(dataHeader);
typeMIME = getTypeMIME(contentType);
subTypeMIME = getSubTypeMIME(contentType);
}
getDataSection();
if(isFile && fileName.length() > 0)
{
if(m_deniedFilesList.contains(fileExt))
throw new SecurityException("The extension of the file is denied to be uploaded (1015).");
if(!m_allowedFilesList.isEmpty() && !m_allowedFilesList.contains(fileExt))
throw new SecurityException("The extension of the file is not allowed to be uploaded (1010).");
if(m_maxFileSize > (long)0 && (long)((m_endData - m_startData) + 1) > m_maxFileSize)
throw new SecurityException(String.valueOf((new StringBuffer("Size exceeded for this file : ")).append(fileName).append(" (1105).")));
totalFileSize += (m_endData - m_startData) + 1;
if(m_totalMaxFileSize > (long)0 && totalFileSize > m_totalMaxFileSize)
throw new SecurityException("Total File Size exceeded (1110).");
}
if(isFile)
{
newFile = new com.philwong.utility.upload.File();
newFile.setParent(this);
newFile.setFieldName(fieldName);
if (newFileName != null && newFileName.equals("none")){
newFile.setFileName(fileName);
newFile.setOldFileName("");
}else {
newFile.setFileName(newFileName);
newFile.setOldFileName(fileName);
}
newFile.setFileExt(fileExt);
newFile.setFilePathName(filePathName);
newFile.setIsMissing(filePathName.length() == 0);
newFile.setContentType(contentType);
newFile.setContentDisp(contentDisp);
newFile.setTypeMIME(typeMIME);
newFile.setSubTypeMIME(subTypeMIME);
if(contentType.indexOf("application/x-macbinary") > 0)
m_startData = m_startData + 128;
newFile.setSize((m_endData - m_startData) + 1);
newFile.setStartData(m_startData);
newFile.setEndData(m_endData);
m_files.addFile(newFile);
} else
{
String value = new String(m_binArray, m_startData, (m_endData - m_startData) + 1);
m_formRequest.putParameter(fieldName, value);
}
if((char)m_binArray[m_currentIndex + 1] == '-')
break;
m_currentIndex = m_currentIndex + 2;
} while(true);
}
黑马 2003-04-06
  • 打赏
  • 举报
回复
学习

81,092

社区成员

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

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