用jsp做了一个新闻发布系统,在后台管理页面中,有一个管理员添加新闻的页面,在这个页面中,管理员发布的图片的路径保存在数据库中,图片文件放在站点的pic文件夹中,但是如果管理员发布的图片的名字与pic文件夹中的一个图片文件同名的话,就会替换原来的图片 文件把,怎么样才能把管理员上传的文件进行重新命名呢,最好是根据当前的时间命名,以保证上传的图片不会出现同名的现象,着急!!!!!

解决方案 »

  1.   

    你可以对上传生成的文件进行重命名,可以在原文件名前添加一个时间,只要在程序中java.sql.Time time=new java.sql.Time();就可以了
      

  2.   

    Date currentTime = new Date();
    SimpleDateFormat formatter = new SimpleDateFormat(sformat);
    String dateString = formatter.format(currentTime);
    以dateString 为文件名就可以了
      

  3.   

    Date currentTime = new Date(); 
    SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddhhmmss"); 
    String dateString = formatter.format(currentTime); 
      

  4.   

    我现在获取上传文件的方法如下:
    <%@ 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=='\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 =endPosition;
    int j=1;
    while((>=0)&&(j<=6)){
    --;
    random.seek();
    n=random.readByte();
    if(n=='\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>
    怎样才能把上传来的图片文件名更改为以当前时间进行命名的文件啊!!!!!!!!!!!
      

  5.   

    呵呵,你太死脑筋了吧,你把你的 Random mynum=new Random(1);
     int num=mynum.nextInt(10000);
     num=(int)Math.random()*10000;
    String dateString = formatter.format(currentTime); 
    fileName=dateString+num;
    然后保存的时候一用把这
    fileName当成保存路径,不过记得加上前边的文件夹什么的名字,下边是我写的上传图片的,你可以看下:<%@ page contentType="text/html;charset=gb2312"%>
    <%@ page import="java.sql.*"%>
    <%@ page import="java.util.*"%>
    <%@ page import="java.io.*"%>
    <%@ page import="com.jspsmart.upload.*" %>
    <%
    //实例化上载bean
    com.jspsmart.upload.SmartUpload mySmartUpload=new com.jspsmart.upload.SmartUpload();
    //初始化
    mySmartUpload.initialize(pageContext);
    //设置上载的最大值
    mySmartUpload.setMaxFileSize(500 * 1024*1024);
    //上载文件
    mySmartUpload.upload();
    //循环取得所有上载的文件
    for (int i=0;i<mySmartUpload.getFiles().getCount();i++)
    {
    //取得上载的文件
    com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i);
    if (!myFile.isMissing())
    {
    //取得上载的文件的文件名
    String myFileName=myFile.getFileName();
    //取得不带后缀的文件名
    String suffix=myFileName.substring(0,myFileName.lastIndexOf('.'));
    //取得后缀名
    String ext= mySmartUpload.getFiles().getFile(0).getFileExt();
    if(!(ext.equals("gif")||ext.equals("jpg")||ext.equals("wmv")||ext.equals("avi")||ext.equals("swf"))){
    out.print("<script language='javascript'>alert('上传失败,图片格式为GIF或者JPG,视频格式为WMV或者AVI,falsh为swf请重试!');</script>");
    out.print("<script language='javascript'>window.location.href = 'uploadb.jsp'</script>");}else
    {//取得文件的大小
    int fileSize=myFile.getSize();
    //保存路径
    String aa=getServletContext().getRealPath("/")+"upload/";
     Random mynum=new Random(1);
     int num=mynum.nextInt(10000);
     num=(int)Math.random()*10000;
    java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyMMddhhmmss");java.util.Date date=new java.util.Date();
    String days=formatter.format(date);
    days=days+num;
    //System.out.println("days="+days);
    //System.out.println("pre="+pre);
    //取得别的参数
    request.setCharacterEncoding("GB2312");
    /*if(request.getSession(true).getAttribute("id")!="")
    {
     id = request.getSession(true).getAttribute("id").toString();
    }myFile.saveAs("upload/"+days+myFile.getFileName());
    String pic="upload/"+days+myFile.getFileName();*/
    myFile.saveAs("upload/"+days+"6."+ext);
    String pic="upload/"+days+"6."+ext;session.setAttribute("pic",pic);
    out.print("<script language='javascript'>parent.heheForm.pic.value='"+pic+"'; </script>");
    out.print("<script language='javascript'>alert('上传成功');</script>");
    //下面的是将上载的文件保存到数据库中//将文件保存在服务器端
    //将文件写到数据库中
    //response.sendRedirect("uploada.jsp");
    }}else{out.print("<script language='javascript'>alert('文件上传失败!');location=''</script>");}}//与前面的if对应
    %>
    <html>
    <head></head>
    <body leftmargin="0" topmargin="0" bgcolor="#ffffff">
    <form>
    <input type="text" name="pic" value="<%=session.getAttribute("pic")%>" size="30">
    </form>
    </body>
    </html>
      

  6.   

    用时间作为文件名的时候也要注意数据并发问题,避免不同用户同时获得了相同的时间。
    转换文件名,你只要在Output的时候指定新的文件名就好了啊汗