用jsp做了一个新闻发布系统,在后台管理页面中,有一个管理员添加新闻的页面,在这个页面中,管理员发布的图片的路径保存在数据库中,图片文件放在站点的pic文件夹中,但是如果管理员发布的图片的名字与pic文件夹中的一个图片文件同名的话,就会替换原来的图片 文件把,怎么样才能把管理员上传的文件进行重新命名呢,最好是根据当前的时间命名,以保证上传的图片不会出现同名的现象,着急!!!!!你可以对上传生成的文件进行重命名,可以在原文件名前添加一个时间,只要在程序中java.sql.Time time=new java.sql.Time();就可以了Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat(sformat);
String dateString = formatter.format(currentTime);
以dateString 为文件名就可以了Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddhhmmss");
String dateString = formatter.format(currentTime); 楼上说得方法都是获取当前时间的,具体应该怎样把上传的图片以当前时间命名呢我现在获取上传文件的方法如下: 数据挖掘实验室
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<% request.setCharacterEncoding("gb2312"); %>
<%@ page import="java.io.*"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title> </title>
</head>
<body>
<%
try{
String temp=(String)session.getId();
File f1=new File((String)request.getRealPath("photo")+"/",temp);
out.println(f1);
FileOutputStream o=new FileOutputStream(f1);
out.println(o);
InputStream in=request.getInputStream();
byte b[]=new byte[10000000];
int n;
while((n=in.read(b))!=-1){ 数据挖掘交友
o.write(b,0,n);
}
o.close();
in.close();
//读取临时文件f1,从中获取上传文件的名字和上传文件的内容。
RandomAccessFile random=new RandomAccessFile(f1,"r");
int second=1;
String secondLine=null;
while(second <=2){
secondLine=random.readLine();
second++;
}
int position=secondLine.lastIndexOf('\');
String fileName=new String((secondLine.substring(position+1,secondLine.length()-1)).getBytes("iso-8859-1"), "GB2312");
random.seek(0);
long forthEnPosition=0;
int forth=1;
while((n=random.readByte())!=1&&(forth <=4)){
if((n=='
')){
forthEnPosition=random.getFilePointer();
forth++;
}
}
File f2=new File((String)request.getRealPath("photo")+"/",fileName);
session.setAttribute("Name",fileName); 数据挖掘实验室
RandomAccessFile random2=new RandomAccessFile(f2,"rw");
random.seek(random.length());
long endPosition=random.getFilePointer();
long mark=endPosition;
int j=1;
while((mark> =0)&&(j <=6)){
mark--;
random.seek(mark);
n=random.readByte();
if(n=='
'){
endPosition=random.getFilePointer();
j++;
}
}
random.seek(forthEnPosition);
long startPoint=random.getFilePointer();
while(startPoint <endPosition-1){
n=random.readByte();
random2.write(n);
startPoint=random.getFilePointer();
}
random2.close();
random.close();
f1.delete();
out.println(" <script language='javascript'> alert('上传成功');window.close();opener.frm.Photo.value='../photo/"+fileName+"'; </script> "); 数据挖掘论坛
}catch(IOException e){
out.println("文件上传失败");
}
%>
</body>
</html>
怎样才能把上传来的图片文件名更改为以当前时间进行命名的文件啊!!!!!!!!!!!Create By Any-Extract(WL-AE)