ssh頭像上傳
1. SSH 圖片上傳問題 java.lang.NullPointerException myFileName為空,那個圖片的名字獲取不過來
private File myFile;
private String myFileName; ->應該為 private String myFileFileName;
上傳時文件名應該是  文件+「FileName」  在這里就是 myFile+「FileName」
2. ssh 上傳圖片 只保存圖片名稱在資料庫怎麼做
這很簡單啊,一般情況下上傳文件都是提供一個文件選擇框,也就是<input type="file" />給用戶,用戶選擇文件後,瀏覽器會自動將文件名顯示在文本框中,你只要將這個文件名取出來然後保存到資料庫中就可以了
3. ssh上傳圖片的問題
存鏈接,圖片存硬碟里的
4. 在SSH框架中建表照片用什麼類型,就想寫個學生信息系統,然後上傳學生頭像
用varchar存地址不就行了!
5. ssh框架上傳圖片與顯示
頁面的form
<formaction="indexDetail.action"method="post"enctype="multipart/form-data">
圖片:<inputtype="file"name="file"/><br/>
<inputtype="submit"name="imageField"class="btn_imgimg_submit"value="提交"/>
</form>
後台代碼
//上傳文件
privateFilefile;
privateStringfileFileName;
Stringrealpath=ServletActionContext.getServletContext().getRealPath("/images/ask/");
if(!newFile(realpath).exists()){
newFile(realpath).mkdir();
}
String[]typechoose=fileFileName.split("\.");
intichoose=typechoose.length;
Stringtype=ichoose>1?typechoose[ichoose-1]:"";
SimpleDateFormatsmat=newSimpleDateFormat("yyyyMMddHHmmss");
Stringnewfilname=smat.format(newDate())+"."+type;
Stringpath=realpath+"/"+newfilname;
FileUtil.saveFile(path,file);
核心代碼都在裡面
6. SSH2框架圖片上傳到資料庫並顯示在JSP頁面
//傳統的struts2上傳是很簡單的。
//頁面form提交到action:
//這里使用集合,頁面提交過來的n個inputname=「file」的文件將會被裝進去,如果只上傳一
//個文件可以直接聲明成:privateFilefile、StringfileFileName、StringfileContentType
privateList<File>file;
privateList<String>fileFileName;
privateList<String>fileContentType;
publicList<File>getFile(){
returnfile;
}
publicvoidsetFile(List<File>file){
this.file=file;
}
publicList<String>getFileFileName(){
returnfileFileName;
}
publicvoidsetFileFileName(List<String>fileFileName){
this.fileFileName=fileFileName;
}
publicList<String>getFileContentType(){
returnfileContentType;
}
publicvoidsetFileContentType(List<String>fileContentType){
this.fileContentType=fileContentType;
}
publicStringexecute()throwsException{
List<String>s=newArrayList<String>();
for(inti=0;i<file.size();i++){
InputStreamis=newFileInputStream(file.get(i));
//在webroot先建立個upload文件夾,也可以用代碼創建,這里為了簡便,就直接使用了
Stringroot=ServletActionContext.getRequest().getRealPath("/upload");
FiledistFile=newFile(root,this.getFileFileName().get(i));
OutputStreamos=newFileOutputStream(distFile);
byte[]buffer=newbyte[400];
intlength=0;
while((length=is.read(buffer))>0){
os.write(buffer,0,length);
}
is.close();
os.close();
//資料庫存放以下路徑,當需要在頁面顯示,直接提取出來用IMG標簽裝載即可
StringnewFilePath=root+"/"+distFile.getFileName();
}
returnSUCCESS;
}
//的代碼不需要很復雜,簡單的執行資料庫插入就好。
7. ssh框架中圖片無法上傳了,怎麼解決
也許地址有問題。你自己看看地址。還有就是在配置文件中沒設置
8. 能不能個我一段 SSH 上傳圖片的代碼!只將圖片的路徑保存到資料庫中!
給你一段!希望能幫到你。
/**
  * @see跳轉方法
  * @param mapping
  * @param form
  * @param request
  * @param response
  * @return
  * @throws IOException
  */
 public ActionForward toadd(ActionMapping mapping,ActionForm form,
   HttpServletRequest request,HttpServletResponse response) throws IOException
 {
  HoteForm hoteForm = (HoteForm) form;
  saveToken(request);//生成一個TOKEN 
  return mapping.findForward("add");
 }
 
 /**
  * @see執行添加方法
  * @param mapping
  * @param form
  * @param request
  * @param response
  * @return
  * @throws IOException 
  */
 public ActionForward doadd(ActionMapping mapping,ActionForm form,
   HttpServletRequest request,HttpServletResponse response) throws IOException
 {
  HoteForm hoteForm = (HoteForm) form;
  if(this.isTokenValid(request,true))
  {
    //上傳文件
   String dir = this.getServlet().getServletContext().getRealPath("/upLoadFiles/hoteimg");
   if(form instanceof HoteForm)
   {
    FormFile file = hoteForm.getTheFile();
    //定義圖片格式
    List<String> list = new ArrayList();
    list.add("jpg");
    list.add("jpeg");
    list.add("gif");
    list.add("bmp");
    if(!dir.endsWith("/"))
    {
     dir = dir.concat("/");
     if(file != null)
     {
      String fileName = file.getFileName();//獲取文件名
      int fsize = file.getFileSize();//獲取文件大小
      String ext = fileName.substring(fileName.lastIndexOf(".")+1,fileName.length());// 獲取文件類型,即擴展名
      fileName = getdateRenameFileName()+"."+ext;//文件重命名
      ext = ext.toLowerCase();//轉換為小寫
      //判斷是否是要求的上傳的類型圖片
      if(!list.contains(ext))
      {
       response.getWriter().print("<script>alert('不支持該文件!');location.href='hote.do?operate=toadd'</script>");
       return null;
      }
      if(fsize > 1024*1024)
      {
       response.getWriter().print("<script>alert('文件過大!');location.href='hote.do?operate=toadd'</script>");
       return null;
      }
      try {
       InputStream stream = file.getInputStream();
       OutputStream bos = new FileOutputStream(dir+"/"+fileName);
       int bytesRead = 0;
       byte[] buffer = new byte[8192];
       while((bytesRead = stream.read(buffer,0,8192)) != -1)
       {
        bos.write(buffer,0,bytesRead);
       }
       bos.close();
       
      } catch (Exception e) {
       // TODO: handle exception
      }
      file.destroy();
      hoteForm.getHote().setImage(fileName);
     }
    }
}
   
        // 獲取系統當前時間
   java.util.Date date = Calendar.getInstance().getTime();
   // 時間格式化 年-月-日
   String time = new SimpleDateFormat("yyyy-MM-dd").format(date);
   hoteForm.getHote().setTime(time);
   Users user = (Users)request.getSession().getAttribute("user");
   hoteForm.getHote().setUsers(user);
   if(hoteForm.getHote() != null)
   {
    if(this.hoteBiz.add(hoteForm.getHote()))
    {
     response.getWriter().print("<script> alert('添加成功!');location.href='hote.do?operate=getHoteList'</script>");
    }
   }
   response.getWriter().print("<script> alert('添加失敗!');javascript:history.go(-1);</script>");
  }else
  {
   response.getWriter().print("<script> alert('不能重復提交數據!');javascript:history.go(-1);</script>");
  }
  
  return null;
 }
/**
  * @see圖片重命名
  * @return
  */
 public String getdateRenameFileName() {
  java.text.SimpleDateFormat formatter_f = new java.text.SimpleDateFormat(
    "yyyyMMddHHmmss");
  java.util.Date currentTime_f = new java.util.Date(); // 得到當前系統時間
  String new_date_f = formatter_f.format(currentTime_f); // 將日期時間格式化
  return new_date_f;
 }
9. 求一段 SSH上傳圖片的代碼,只需要把路徑存到資料庫中即可
UploadActionForm uaf = (UploadActionForm)form; ///UploadActionForm 是封裝了formbean的java類
  System.out.println("title:"+uaf.getTitle());
  FormFile myFile = uaf.getMyfile();
  if(null != myFile) {
   System.out.println("fileName:"+myFile.getFileName());
   FileOutputStream fos = new FileOutputStream                   ("d:\\+你要上傳到伺服器的路徑);
   fos.write(myFile.getFileData());
   fos.flush();
   fos.close();
  }
10. ssh2使用fileuoload上傳圖片提示問題!上傳成功了但不知怎麼提示上傳圖片過大!
要限制大小,在配置中配置就可以。。。。
上傳到哪,由自己決定,因為,上傳後得到文件和輸入流了
