javaservlet接收圖片上傳
1. jsp+servlet 上傳圖片並顯示出來
其實你這個擋也顯示圖片其實很簡單的,
你的需求無非是兩個
1.servlet上傳文件(圖片)
2.點擊 瀏覽 圖標,然後選擇圖片文件,然後就可以在頁面中的某個地方看到圖片
是這兩個需求么?
首先說第二個吧。
你上傳圖片之後,就馬上觸發js函數,內容為
var PicPath = document.getElementById("yourfile").value;
document.getElementById("yourDiv").innerHTML="<IMG src="+PicPath+"/>";
OK了
第一個嘛就無所謂說了,不過我還是貼一個代碼吧,
public void upLoadFile(HttpServletRequest request, HttpServletResponse response) {
  PrintWriter out = null;
  response.setCharacterEncoding("UTF-8");
  //實例化文件工廠
        FileItemFactory factory = new DiskFileItemFactory();
        //配置上傳組件ServletFileUpload  
        ServletFileUpload upload = new ServletFileUpload(factory);   
        try {
         out = response.getWriter();
           //從request得到所有上傳域的列表   
            List<FileItem> list = upload.parseRequest(request); 
            
            for (FileItem item : list) {
             //isFormField判斷一個item類對象封裝的是一個普通的表單欄位還是文件表單欄位。
              // 如果item是文件域,則做出如下處理:
                if (!item.isFormField()) {
                 
                    //上傳文件域的Name 
                    String fileName = item.getName();
                    
                    //截取擴展名
                    int idx = fileName.lastIndexOf(".");   
                    String extension = fileName.substring(idx);  
                    
                    //獲取文件名
                    String name = new Date().getTime() + extension;
                    
                    //得到文件夾的物理路徑
                    String path = this.getServletContext().getRealPath("\\upload");   
                    
                   //創建一個File
                    File file = new File(path + "\\" + name);   
                    FileOutputStream o = new FileOutputStream(file);
                    InputStream in = item.getInputStream();
                    try {   
                     LoadProcessServlet.process = 0;
                        LoadProcessServlet.total = 100;
                        LoadProcessServlet.isEnd = false;
                     LoadProcessServlet.total = item.getSize();
                     byte b[] = new byte[1024];
                        int n;
                        while ((n = in.read(b)) != -1) {
                         LoadProcessServlet.process+=n;
                            o.write(b, 0, n);
                            System.out.println("實際:"+LoadProcessServlet.process);
                        }
                    } catch (Exception e) {   
                        e.printStackTrace();   
                    }finally{
                     LoadProcessServlet.isEnd = true;
                    }
                }   
            }
        } catch (Exception e) {   
            e.printStackTrace();   
        }   
  
 }
2. java接收from表單提交的多圖片,怎麼點擊提交的時候一起上傳
首先,文本類的可以放在request中通過request.getAttribute(name)獲取。圖片你在前端放地址,後端也是像前面通過request.getAttribute(name)獲取後存入資料庫。這是jsp+servlet的做法。jsp有九大內置對象用於傳遞數據。而你如果用spring+springmvc的話是通過參數綁定來傳遞數據的。詳細的你可以了解框架文檔。建議你選擇一種框架可以便捷開發。jsp+servlet是比較原始的處理方式。
3. java的servlet如何將圖片傳到jsp頁面上
servlet裡面有個setAttribute,你用這個給圖片的路徑放進去,在JSP頁面a標簽里用EL表達式顯示出來。
4. 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又做了封裝,使用起來更傻瓜化,很容易掌握。
-----------------------------
          以上回答,如有不明白可以聯系我。
5. java怎樣上傳圖片(寫個例子謝謝);
用struts2框架。。。馬上OK。。。
在響應的ACTION中,只需要定義一個File的變數即可
6. java寫服務端,用servlet接收客戶端上傳的音頻,視頻,圖片,寫到本地硬碟,唯獨圖片打不開
你看看,你設定的輸出流後面的參數是不是吧圖片的後綴給忘記了?往本地硬碟上寫的時候,讀取過來後,寫出去之前是可以更改名稱 和後綴名的
7. java jsp 怎麼上傳圖片到servlet
jsp: 
<form action="up.do" method="post" enctype="multipart/form-data" id="upform">   文件:<input type="file" name="file" id="file"/>   <input type="button" id="btn" value="上傳"/>  </form>
SERVLET:
FileItemFactory fileItemFactory = new DiskFileItemFactory();    ServletFileUpload fileUpload = new ServletFileUpload(fileItemFactory);    //設置  中文 編碼集  fileUpload.setHeaderEncoding("UTF-8");  try {   List<FileItem> fileItems= fileUpload.parseRequest(request);   for (FileItem fi : fileItems) {    if(fi.isFormField()){     }    }   }      for (FileItem fileItem : fileItems) {    if(fileItem.isFormField()){     }else{     //文件名     String filename = fileItem.getName();     //文件名處理     if(filename.indexOf('\\') != -1){      filename = filename.substring(filename.lastIndexOf('\\')+1);     }     System.out.println(filename);          if(filename == null || filename.equals("")){      request.setAttribute("msg", "沒有指定上傳文件");      request.getRequestDispatcher("index.jsp").forward(request, response);      //驗證碼錯誤  就退出      return;     }         //path   生成文件夾     String fpath = "F:\\\\中軟高科\\\\小組\\\\5組日報"+UpFileUtil.getMonthadnDate();     File file = new File(fpath);     if(!file.exists()){      file.mkdir();     }          //文件全路徑      fpath = fpath+"\\\\"+filename;//     System.out.println(filename+"--->"+fpath+"--->"+UpFileUtil.getTime());              //寫出文件到   伺服器磁碟     InputStream in = fileItem.getInputStream();//request.getInputStream();     OutputStream out = new FileOutputStream(fpath);     byte[] buff = new byte[1024*10];     while(in.read(buff) != -1){      out.write(buff);     }          out.flush();     out.close();     in.close();     //保存文件信息到資料庫     new UpFileDao().updFile("{call file_add(?,?,?)}",new String[]{filename,fpath,UpFileUtil.getTime()});          filename = null;     fpath = null;     file = null;     request.setAttribute("msg", "上傳成功");     request.getRequestDispatcher("index.jsp").forward(request, response);    }   }     } catch (Exception e) {   e.printStackTrace();  }
8. 請問用Java 如何實現圖片上傳功能
自己寫程序來上傳位元組流文件很難的,用SmartUpload.jar包吧,專門用於JSP上傳下載的,唯一缺點就是中文支持不太好,不過你可以改一下原程序的字元集就行了。上網搜,沒有找我!我給你發
9. java servlet如何接收其他程序傳來的圖片和參數!
在jsp頁面中將圖片轉換的二進制數組,Servlet中的用request.getpari........(變數明)來接受即可!不明白再Hi我。我都在線的!
10. java:servlet接收圖片,並把它保存到資料庫中
這種代碼網上不是一大片嗎
publicbooleanstoreImage(Filefile){
try{
//打開文件
FileInputStreamfin=newFileInputStream(file);
//建一個緩沖保存數據
ByteBuffernbf=ByteBuffer.allocate((int)file.length());
byte[]array=newbyte[1024];
intoffset=0,length=0;
//讀存數據
while((length=fin.read(array))>0){
if(length!=1024)nbf.put(array,0,length);
elsenbf.put(array);
offset+=length;
}
//關閉文件
fin.close();
//新建一個數組保存要寫的內容
byte[]content=nbf.array();
Stringsql="insertintoimages(bin_data)values(?)";
PreparedStatementpstmt=conn.prepareStatement(sql);
pstmt.setBytes(1,content);
pstmt.execute();
pstmt.close();
}catch(Exceptione){
e.printStackTrace();
returnfalse;
}
returntrue;
}
					