2011-06-04 93 views
2

我有一些代碼上傳圖像到服務器與struts2.After上傳我想顯示我的圖像作爲縮略圖圖像,併發送圖像的路徑,我已經上傳到database.I混淆瞭如何使它可以工作上傳圖像並保存Url與Struts

這是我的Action Code.Cimande Action是ActionSupport的擴展。

package com.maetrika.jagatoko.controller; 

import java.io.File; 
import java.io.FilePermission; 

import javax.servlet.http.HttpServletRequest; 

import org.apache.commons.io.FileUtils; 
import org.blueoxygen.cimande.commons.CimandeAction; 

public class FileUpload extends CimandeAction{ 

    private File upload; 
    private String uploadContentType; 
    private String uploadFileName; 
    private String fileCaption; 

    private HttpServletRequest servletRequest; 

    File file; 

    public String execute() throws Exception{ 

     try{ 

     //String filePath=servletRequest.getRealPath("/"); 
      String filePath=servletRequest.getSession().getServletContext().getRealPath("/"); 
      File fileToCreate=new File(filePath, this.uploadFileName); 
      FileUtils.copyFile(upload, fileToCreate); 
     }catch (Exception e) { 
      e.printStackTrace(); 
      return INPUT; 
      // TODO: handle exception 
     } 



     return SUCCESS; 
    } 






    public File getUpload() { 

     return upload; 
    } 

    public void setUpload(File upload) { 
     this.upload = upload; 
    } 

    public String getUploadContentType() { 
     return uploadContentType; 
    } 

    public void setUploadContentType(String uploadContentType) { 
     this.uploadContentType = uploadContentType; 
    } 

    public String getUploadFileName() { 

     System.out.println("=======================" + uploadFileName); 
     return uploadFileName; 

    } 

    public void setUploadFileName(String uploadFileName) { 
     this.uploadFileName = uploadFileName; 
    } 

    public String getFileCaption() { 
     return fileCaption; 
    } 

    public void setFileCaption(String fileCaption) { 
     this.fileCaption = fileCaption; 
    } 



    public HttpServletRequest getServletRequest() { 
     return servletRequest; 
    } 



    public void setServletRequest(HttpServletRequest servletRequest) { 
     this.servletRequest = servletRequest; 
    } 



    public File getFile() { 
     return file; 
    } 



    public void setFile(File file) { 
     this.file = file; 
    } 


} 

我使用速度進行查看。 這是我上傳圖片的代碼

<html> 
<head> 
</head> 

<body> 
    <form method="post" action="doUpload" enctype="multipart/form-data"> 
     <table border="0" cellpadding="2"> 
      <tr> 
       <td>Upload Gambar Produk</td> 
      </tr> 

      <tr> 
       <td><input type="file" name="upload"></td> 
      </tr> 

      <tr> 
       <td><input type="submit" value="submit"/> 
      </tr> 
     </table> 

    </form> 
</body> 
</html> 

我是Java的新手programming.Help我請..

+0

您可以嘗試** [圖像插件](https://cwiki.apache.org/S2PLUGINS/image-plugin.html)**(第三黨)的Struts2。 – lschin 2011-06-12 04:55:23

+0

我們如何在android中爲.net服務器做到這一點? – Kishore 2011-10-10 11:49:59

回答

2

你必須DATABSE僅存儲圖像的名字,以毫秒爲單位的時間戳產生以避免重複項目

String fileName = myFile.getFileName(); 
    String extension = ""; 

    if (fileName != null && !fileName.equals("")) { 
     extension = fileName.substring(fileName.lastIndexOf("."), fileName.length()); 
    } 

    long longName = Calendar.getInstance().getTimeInMillis(); //date in miliseconds 
    String newFileName = longName + extension; 

現在您想要將圖像存儲在您的項目中以備將來使用。要做到這一點,讓你的根文件夾,文件夾命名userimages,你的動作代碼將看起來像:

String uploadFolder = "userimages"; 
String filePath = getServlet().getServletContext().getRealPath("/") + uploadFolder; 

/* Save file on the server */ 
    if (!fileName.equals("")) { 
     System.out.println("Server path:" + filePath); 

     //Create file 
     File fileToCreate = new File(filePath, newFileName); 

     //If file does not exists, create file 
     if (!fileToCreate.exists()) { 
      FileOutputStream fileOutStream = new FileOutputStream(fileToCreate); 
      fileOutStream.write(myFile.getFileData()); 
      fileOutStream.flush(); 
      fileOutStream.close(); 
     } 

    } 

現在,在你的JSP的顯示出來的圖像,你需要使用EL

東西這樣的:

<img src="${pageContext.request.contextPath}/userimages/${imagename}"/> 

我希望幫你