java上傳圖片並顯示
給你簡單的描述一下:上傳的圖片保存在WebRoot下,在WebRoot下建立專門保存的文件夾,每上傳一張圖片數據保存圖片的路徑以及圖片的一些其他信息,你說的讀取直接查出路徑,用<img src="路徑">即可顯示
Ⅱ java web開發,上傳圖片並讀取
java web開發中,使用文件操作類來上傳圖片並讀取,如下代碼:
*@desc:圖片處理工具
*@author:bingye
*@createTime:2015-3-17下午04:25:32
*@version:v1.0
*/
publicclassImageUtil{
/**
*將圖片寫到客戶端
*@author:bingye
*@createTime:2015-3-17下午04:36:04
*@history:
*@paramimage
*@paramresponsevoid
*/
publicstaticvoidwriteImage(byte[]image,HttpServletResponseresponse){
if(image==null){
return;
}
byte[]buffer=newbyte[1024];
InputStreamis=null;
OutputStreamos=null;
try{
is=newByteArrayInputStream(image);
os=response.getOutputStream();
while(is.read(buffer)!=-1){
os.write(buffer);
os.flush();
}
}catch(IOExceptione){
e.printStackTrace();
}finally{
try{
if(is!=null){is.close();}
if(os!=null){os.close();}
}catch(IOExceptione){
e.printStackTrace();
}
}
}
/**
*獲取指定路勁圖片
*@author:bingye
*@createTime:2015-3-21上午10:50:44
*@paramfilePath
*@paramresponsevoid
*/
publicstaticvoidwriteImage(StringfilePath,HttpServletResponseresponse){
FileimageFile=newFile(filePath);
if(imageFile!=null&&imageFile.exists()){
byte[]buffer=newbyte[1024];
InputStreamis=null;
OutputStreamos=null;
try{
is=newFileInputStream(imageFile);
os=response.getOutputStream();
while(is.read(buffer)!=-1){
os.write(buffer);
os.flush();
}
}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}finally{
try{
if(is!=null){is.close();}
if(os!=null){os.close();}
}catch(IOExceptione){
e.printStackTrace();
}
}
}
}
/**
*圖片上傳到文件夾
*@author:bingye
*@createTime:2015-3-20下午08:07:25
*@paramfile
*@paramsavePath
*@returnboolean
*/
(CommonsMultipartFilefile,StringsavePath){
if(file!=null&&!file.isEmpty()){
//獲取文件名稱
StringfileName=file.getOriginalFilename();
//獲取後綴名
StringsuffixName=fileName.substring(fileName.indexOf(".")+1);
//新名稱
StringnewFileName=System.currentTimeMillis()+"."+suffixName;
//新文件路勁
StringfilePath=savePath+newFileName;
//獲取存儲文件路徑
FilefileDir=newFile(savePath);
if(!fileDir.exists()){
//如果文件夾沒有:新建
fileDir.mkdirs();
}
FileOutputStreamfos=null;
try{
fos=newFileOutputStream(filePath);
fos.write(file.getBytes());
fos.flush();
returnResultUtil.success("UPLOAD_SUCCESS",URLEncoder.encode(newFileName,"utf-8"));
}catch(Exceptione){
e.printStackTrace();
returnResultUtil.fail("UPLOAD_ERROR");
}finally{
try{
if(fos!=null){
fos.close();
}
}catch(IOExceptione){
e.printStackTrace();
returnResultUtil.fail("UPLOAD_ERROR");
}
}
}
returnResultUtil.fail("UPLOAD_ERROR");
}}
Ⅲ java怎麼上傳圖片然後顯示在JSP頁面中
java上圖片和上傳文件的方式是一樣的... 都是將文件上傳到指定目錄後,地址放入資料庫中.
這種方式, 只需要學習下 apache中的common下的upload包 操作即可.
如果是struts, 帶有這個jar包的
Ⅳ java實現圖片上傳至伺服器並顯示,如何做希望要具體的代碼實現
很簡單。
可以手寫IO讀寫(有點麻煩)。
怕麻煩的話使用FileUpload組件 在servlet里doPost嵌入一下代碼
public void doPost(HttpServletRequest request,HttpServletResponse response)
  throws ServletException,IOException{
   response.setContentType("text/html;charset=gb2312");
   PrintWriter out=response.getWriter();
   
   //設置保存上傳文件的目錄
   String uploadDir =getServletContext().getRealPath("/up");
   System.out.println(uploadDir);
    if (uploadDir == null)
    {
     out.println("無法訪問存儲目錄!");
     return;
    }
    //根據路徑創建一個文件
   File fUploadDir = new File(uploadDir);
    if(!fUploadDir.exists()){
     if(!fUploadDir.mkdir())//如果UP目錄不存在 創建一個 不能創建輸出...
     {
      out.println("無法創建存儲目錄!");
      return;
     }
    }
 
    if (!DiskFileUpload.isMultipartContent(request))
    {
     out.println("只能處理multipart/form-data類型的數據!");
     return ;
    }
 
   DiskFileUpload fu = new DiskFileUpload();
   //最多上傳200M數據
   fu.setSizeMax(1024 * 1024 * 200);
   //超過1M的欄位數據採用臨時文件緩存
   fu.setSizeThreshold(1024 * 1024);
   //採用默認的臨時文件存儲位置
   //fu.setRepositoryPath(...);
   //設置上傳的普通欄位的名稱和文件欄位的文件名所採用的字元集編碼
   fu.setHeaderEncoding("gb2312");
   
   //得到所有表單欄位對象的集合
   List fileItems = null;
   try
   {
    fileItems = fu.parseRequest(request);//解析request對象中上傳的文件
    
   }
   catch (FileUploadException e)
   {
    out.println("解析數據時出現如下問題:");
    e.printStackTrace(out);
    return;
   }
 
  //處理每個表單欄位
  Iterator i = fileItems.iterator();
  while (i.hasNext())
  {
   FileItem fi = (FileItem) i.next();
   if (fi.isFormField()){
    String content = fi.getString("GB2312");
    String fieldName = fi.getFieldName();
    request.setAttribute(fieldName,content);
   }else{
     try
     {
      String pathSrc = fi.getName();
      if(pathSrc.trim().equals("")){
       continue;
      }
     int start = pathSrc.lastIndexOf('\\');
     String fileName = pathSrc.substring(start + 1);
     File pathDest = new File(uploadDir, fileName);
     
     fi.write(pathDest);
     String fieldName = fi.getFieldName();
     request.setAttribute(fieldName, fileName);
   }catch (Exception e){
     out.println("存儲文件時出現如下問題:");
     e.printStackTrace(out);
     return;
    }
   finally //總是立即刪除保存表單欄位內容的臨時文件
   {
    fi.delete();
   }
 
  }
 }
注意 JSP頁面的form要加enctype="multipart/form-data" 屬性, 提交的時候要向伺服器說明一下 此頁麵包含文件。
如果 還是麻煩,乾脆使用Struts 的上傳組件 他對FileUpload又做了封裝,使用起來更傻瓜化,很容易掌握。
-----------------------------
          以上回答,如有不明白可以聯系我。
Ⅳ JAVA上傳圖片到項目文件夾,怎麼顯示在頁面上啊
1、你首先要獲取到該文件夾路徑
2、在jsp頁面中引用img標簽,然後利用src將路徑傳進去
3、即可顯示在頁面
Ⅵ java實現圖片上傳並顯示
file inputstream outputstream ,基本上IO流章節實現,當然還有很多封裝的jar包,網上去搜搜。另外,圖片上傳你還需一個web層
Ⅶ java實現圖片上傳至伺服器並顯示,如何做
給你段代碼,是用來在ie上顯示圖片的(servlet):
public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
      String id = request.getParameter("id");
       File file = new File(getServletContext().getRealPath("/")+"out"+"/"+id+".gif");
         response.setCharacterEncoding("gb2312");
         response.setContentType("doc");
         response.setHeader("Content-Disposition", "attachment; filename=" + new String(file.getName().getBytes("gb2312"),"iso8859-1"));
         
         System.out.println(new String(file.getName().getBytes("gb2312"),"gb2312"));
         
         OutputStream output = null;
         FileInputStream fis = null;
         try
         {
             output  = response.getOutputStream();
             fis = new FileInputStream(file);
             byte[] b = new byte[1024];
             int i = 0;
           
             while((i = fis.read(b))!=-1)
             {
                 
                 output.write(b, 0, i);
             }
             output.write(b, 0, b.length);
            
             output.flush();
             response.flushBuffer();
         }
         catch(Exception e)
         {
             System.out.println("Error!");
             e.printStackTrace();
         }
         finally
         {
             if(fis != null)
             {
                 fis.close();
                 fis = null;
             }
             if(output != null)
             {
                 output.close();
                 output = null;
             }
         }
 }
這個程序的功能是根據傳入的文件名(id),來為瀏覽器返回圖片流,顯示在<img>標簽里
標簽的格式寫成如下:
<img src="http://localhost:8080/app/preview?id=111 "/><br/>
顯示的是111.gif這個圖片
你上面的問題:
1.我覺得你的第二個辦法是對的,我們也是這樣做的,需要的是把資料庫的記錄id號傳進servlet,然後讀取這條記錄中的路徑信息,生成流以後返回就是了
關於上傳文件的問題,我記得java中應該專門有個負責文件上傳的類,你調用就行了,上傳後存儲在指定的目錄里,以實體文件的形式存放
你可以參考這個:
http://blog.csdn.net/arielxp/archive/2004/09/28/119592.aspx
回復:
1.是的,在response中寫入流就行了
2.是發到servlet中的,我們一般都是寫成servlet,短小精悍,使用起來方便,struts應該也可以,只是我沒有試過,恩,你理解的很對
Ⅷ java如何實現上傳圖片到伺服器,並預覽圖片
預覽,不能簡單的用<img>,然後把地址賦給src的,會出現瀏覽器不兼容問題
用css濾鏡,像下面
document.getElementById("previewImg").style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod='scale',src='" + o.value + "')";
至於如何上傳,struts2很方便的
Ⅸ java上傳圖片,能在頁面上顯示
可以, 代碼要敲的, 給你說原理吧, 上傳---->伺服器(網路資料), 返迴文件路徑通過ajax更申請文件一列內容, 載入圖片就ok了!!!
Ⅹ JAVA本地上傳圖片到伺服器,完事圖片直接存到伺服器下的一個文件夾里。我想把圖片直接顯示出來。
如果是web程序,就在頁面上放圖<img src='伺服器域名/保存的文件路徑名/文件名' />
如果是窗體程序,就要在顯示界面上加入圖形顯示控制項,放入圖片文件的完整路徑
