2014-04-15 35 views
1
<body> 
    <form method="post" action="DemoServlet" enctype="multipart/form-data" name="form1"> 
     <input type="file" name="file" /> 
     Image_Name:<input type="text" name="file"/> 
     <input type="submit" value="Go"/> 
    </form> 
</body> 

這是我的index.jsp頁面。 這個Servlet是DemoServlet,當用戶點擊提交按鈕時它會在這裏。而在jsp頁面中,假設用戶給出的Image_Name是IPL,圖像的實際名稱是funny.jpg,然後在保存圖像時它應該存儲爲IPL.png,在這裏我能夠與funny.jpg正確上傳圖片,但我需要的圖像保存爲給定的名稱在index.jsp頁面如何在上傳並保存文件夾時使用java chage圖像名稱?

public class DemoServlet extends HttpServlet { 

protected void doPost(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException { 
    Date date = new Date(); 
    response.setContentType("text/html"); 
    PrintWriter out = response.getWriter(); 
    boolean isMultiPart = ServletFileUpload.isMultipartContent(request); 
    if (isMultiPart) { 
     ServletFileUpload upload = new ServletFileUpload(); 
     try { 
      FileItemIterator itr = upload.getItemIterator(request); 
      while (itr.hasNext()) { 
       FileItemStream item = itr.next(); 
       if (item.isFormField()) { 
        String fieldname = item.getFieldName(); 
        InputStream is = item.openStream(); 
        byte[] b = new byte[is.available()]; 
        is.read(b); 
        String value = new String(b); 
        response.getWriter().println(fieldname + ":" + value + "</br>"); 
       } else { 
        String TempPath = getServletContext().getRealPath(""); 
        String path = TempPath.substring(0, TempPath.indexOf("build")); 
        if (FileUpload.processFile(path, item)) { 
         out.println("File Uploaded on:" + date + "<br>"); 
         response.getWriter().println("Image Upload Successfully"); 
        } else { 
         response.getWriter().println("Failed.....Try again"); 
        } 
       } 
      } 
     } catch (FileUploadException fue) { 
      fue.printStackTrace(); 
     } 
    } 
} 

}

的文本字段,這是Java類

public class FileUpload { 

public static boolean processFile(String path, FileItemStream item) { 
    try { 
     File f = new File(path + File.separator + "web/images"); 
     if (!f.exists()) { 
      f.mkdir(); 
     } 
     File savedFile = new File(f.getAbsolutePath() + File.separator + item.getName()); 
     FileOutputStream fos = new FileOutputStream(savedFile); 
     InputStream is = item.openStream(); 
     int x = 0; 
     byte[] b = new byte[1024]; 
     while ((x = is.read(b)) != -1) { 
      fos.write(b, 0, x); 
     } 
     fos.flush(); 
     fos.close(); 
     return true; 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return false; 
} 

}

任何人都可以指導我如何動態改變這種情況。提前感謝。

回答

1

我不知道Servlet是如何工作的,但我可以給你一個你需要做的事情的概要。

在DemoServlet你需要採取的IMAGE_NAME領域的投入,使您的文件上傳

的參數中的一個
public static boolean processFile(String path, FileItemStream item, String fileName){ 
    //Method Code 
} 

因爲目前你processFile方法是從你的FileItemStream取文件的名稱。你需要將其從更改爲您的實際文件名

File savedFile = new File(f.getAbsolutePath() + File.separator + item.getName()); 

File savedFile = new File(f.getAbsolutePath() + File.separator + fileName + ".png"); 
+0

能不能請你改變我的代碼,並恢復我回來 – tajMahal

+0

如果(FileUpload.processFile(路徑,項目)),在這裏我應該怎麼傳遞參數 – tajMahal

0
 List fileItems = upload.parseRequest(request); 
     Iterator i = fileItems.iterator(); 
     System.out.println("In >>>>>>>>>>>>>>> :: "+fileItems); 
     while(i.hasNext()){     
      FileItem fi = (FileItem) i.next(); 
      System.out.println("Val <<<<>>>>>>:: "+fi); 
      if(fi.isFormField()){ 
       String fieldName = fi.getFieldName(); 
       String val = fi.getString(); 

       System.out.println(fieldName+" :: Val :: "+val); 
      }else{ 
       String fileName = fi.getName(); 

       String root = getServletContext().getRealPath("/"); 
       File path = new File(root+"/uploads"); 
       if (!path.exists()) { 
        boolean status = path.mkdir(); 
       } 
       File uploadFile = new File(path+"/"+fileName); 
       fi.write(uploadFile); 

      } 

在上面的代碼,你可以在任何時候更改文件名,它會自動使用該名稱保存。

+0

假設,如果我上傳的圖像第二時間和圖像的實際名稱是FirstTemple.jpg,因爲我把它命名爲Temple,它是否會保存爲Temple.jpg? – tajMahal

1

您可以在您的java類代碼中更改圖像的名稱。

public class FileUpload { 

public static boolean processFile(String path, FileItemStream item , String name) { 
    try { 
    File f = new File(path + File.separator + "web/images"); 
    if (!f.exists()) { 
     f.mkdir(); 
    } 
    File savedFile = new File(f.getAbsolutePath() + File.separator + item.getName()); // instead of item.getName() you can give your name. 
    FileOutputStream fos = new FileOutputStream(savedFile); 
    InputStream is = item.openStream(); 
    int x = 0; 
    byte[] b = new byte[1024]; 
    while ((x = is.read(b)) != -1) { 
     fos.write(b, 0, x); 
    } 
    fos.flush(); 
    fos.close(); 
    return true; 
} catch (Exception e) { 
    e.printStackTrace(); 
} 
return false; 

}

,你將不得不在法傳遞文件名。
而不是item.getName()你可以給你的名字。

+0

每次我們都不能在代碼中給出名字,而不是在文本字段中給出該名稱必須爲該指定圖像設置的名稱 – tajMahal

+0

是的,這也很好 看到有一個問題有很多解決方案,我只是取消了一個問題, –

+0

你可以改變我的代碼請 – tajMahal

-2
//How does not work in this way?Please tell me another way. 
import java.io.File; 

public class RenameFileExample { 
    public static void main(String[] args) 
    { 

     File oldfile =new File("oldfile.txt"); 
     File newfile =new File("newfile.txt"); 

     File file = new File("oldfilename.png"); 
     file.renameTo(new File("newfilename.png")); 
     System.out.println("Rename To:"+file.getName()); 

     if(oldfile.renameTo(newfile)){ 
      System.out.println("Rename succesful"); 
     }else{ 
      System.out.println("Rename failed"); 
     } 

    } 
} 
+0

相應地增強你的答案的問題,因爲它不是目前的答案是 – ddb

+0

這並不回答這個問題。如果你有自己的問題,請發佈一個新問題(如果相關的話,你應該包含一個鏈接到這個問題)。 – Hulk

相關問題