最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

MySQL+SSM+Ajax上傳圖片問(wèn)題

 更新時(shí)間:2017年03月16日 10:05:42   作者:梁小濤  
本文主要介紹了MySQL+SSM+Ajax上傳圖片問(wèn)題。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧

第一次寫上傳圖片的代碼,碰到很多問(wèn)題。昨天做了整整一天,終于在晚上的時(shí)候成功了。大聲歡呼。

但是,做完之后,還是有很多問(wèn)題想不通。所以在這里也算是寫個(gè)筆記,日后忘記了可以回顧,也算請(qǐng)教各路朋友。(^_^)

 Q.1. 網(wǎng)上說(shuō)Ajax不能上傳文件,但是這個(gè)說(shuō)法并不是很多,也還是有蠻多通過(guò)Ajax上傳文件的分享。

我也沒(méi)有通過(guò)Ajax做出來(lái),最后是通過(guò)AjaxSubmit這個(gè)方法寫的。

 Q.2. AjaxSubmit這個(gè)方法對(duì)文件上傳的大小有默認(rèn)限制吧。我選擇大于100KB的文件上傳就不能成功,小于100KB的就可以成功。

上傳大于100KB的時(shí)候,瀏覽器console返回下面的提示。說(shuō)明他還是執(zhí)行了ajaxSubmit的success方法,并返回textStatus的值為success,但是XMLHttpRequest, 和 errorThrown的responseText返回的HTML代碼內(nèi)容是我在spring-web.xml配置的異常處理視圖網(wǎng)頁(yè)。

js代碼(提交表單事件):

function postImg(){
 if ($.trim($("#imgFile").val()) == "") { 
   alert("請(qǐng)選擇圖片!"); 
   return; 
  } 
 console.log($("#imgFile")[0].files[0].size);//小于100*1024,下面的請(qǐng)求就可以成功
 var option = {
  url : '/cloudnote/user/insertUserPhoto.do',
  type : 'POST',
//  dataType : 'json',
  headers : {"ClientCallMode" : "ajax"}, //添加請(qǐng)求頭部
  success : function(XMLHttpRequest, textStatus, errorThrown){
   console.log(XMLHttpRequest);
   console.log(textStatus);
   console.log(errorThrown);
   console.log("前端輸出上傳成功");
   $("#imgClose").click();
  },
  error: function(XMLHttpRequest, textStatus, errorThrown) {
   console.log(XMLHttpRequest);
   console.log(textStatus);
   console.log(errorThrown);
   console.log("前端輸出上傳失敗"); 
  }
 };
 $("#imgForm").ajaxSubmit(option);
 return false; 
}

前端HTML表單:

<form id="imgForm" > 
  <div class="modal-content">
   <div class="modal-header">
   <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
   <h4 class="modal-title" id="myModalLabel">修改頭像</h4>
  </div>
  <div class="modal-body">
    <input type="file" id="imgFile" name="imgFile"/> 
   <input id="imgId" name="userId" value="${user.id }" style="display:none" />
  </div>
  <div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal" id="imgClose">關(guān)閉</button>
   <button type="button" class="btn btn-primary" onclick="postImg();" id="imgSubmit">上傳</button>
  </div>
 </div>
</form>

下面是后臺(tái)的java代碼(Controller)

//更新用戶頭像
 @RequestMapping(value="/insertUserPhoto.do",method = RequestMethod.POST)
 public void insertUserPhoto(
   HttpServletRequest req, HttpServletResponse res){
  System.out.println("----- 插入圖片 -------");
  try{
   String id = req.getParameter("userId"); 
   System.out.println(id);
   MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) req;
   MultipartFile file = multipartRequest.getFile("imgFile");
   byte[] photo = file.getBytes();
   boolean result = serv.insertUserPhoto(id, photo); 
   res.setContentType("text/html;charset=utf8"); 
   res.getWriter().write("result:" + result);   
  }catch(Exception e){
   e.printStackTrace();
  }
  System.out.println("----- 插入圖片end -------");
 }
 /**
  * 讀取用戶頭像
  * @param req
  * @param res
  */
 @RequestMapping(value="/readPhoto.do", method=RequestMethod.GET)
 public void readPhoto(HttpServletRequest req, HttpServletResponse res){
  System.out.println("------readPohto-----");
  String id = Utils.getSessionUserId(req);
  try {
   User user = serv.selectUserPhoto(id);
   res.setContentType("image/jpeg");
   res.setCharacterEncoding("utf-8"); 
   OutputStream outputStream = res.getOutputStream(); 
   InputStream in = new ByteArrayInputStream(user.getPhoto()); 
   int len = 0; 
   byte[] buf = new byte[1024]; 
   while((len = in.read(buf,0,1024)) != -1){ 
    outputStream.write(buf, 0, len);
   } 
   outputStream.close(); 
  } catch (IOException e) { 
   e.printStackTrace(); 
  } 
  System.out.println("-----readPohto end-----");
  return;
 }

Service實(shí)現(xiàn)類

//查找用戶圖片(頭像)
 public User selectUserPhoto(String id) throws ImageException {
  User user = userDao.findUserById(id);
  if(user == null){
   throw new UserNameException("用戶名不存在!");
  }
  Map<String, Object> data = userDao.selectUserPhoto(id);
  System.out.println(data);
  user.setPhoto((byte[]) data.get("photo"));
  return user;
 }
 //更新用戶圖片(頭像)
 public boolean insertUserPhoto(String userId, byte[] photo) throws ImageException, UserNameException {
  if(userId == null || userId.trim().isEmpty()){
   throw new UserNameException("用戶id不存在");
  }
  User user = userDao.findUserById(userId);
  if(user == null){
   throw new UserNameException("用戶不存在");
  }
  user.setPhoto(photo);
  int n = userDao.updateUserPhoto(user);
  System.out.println("插入圖片:" + n);
  return n==1?true:false; 
 }

實(shí)體類User的photo 是 byte[] 類型的;

數(shù)據(jù)庫(kù)的photo是 longblob:

mapper映射器:

<!-- 更新圖片 -->
 <update id="updateUserPhoto" parameterType="cn.tedu.note.entity.User"> 
  UPDATE user set id = #{id}, photo = #{photo,jdbcType=BLOB} <!-- 這里試了,如果不加jdbcType=BLOB 會(huì)出錯(cuò),雖然不是很理解,但也照做了 --> 
  WHERE id = #{id}
 </update> 
 <!-- 獲取圖片 -->
 <select id="selectUserPhoto" parameterType="String" resultType="Map"> 
   SELECT id as id, photo as photo from user 
   WHERE id=#{id} 
 </select>

Spring-web.xml配置

 <!-- 文件上傳表單的視圖解析器 --> 
 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 
  <property name="maxUploadSize"><value>100000</value></property> 
  <property name="defaultEncoding"><value>UTF-8</value></property> 
 </bean> 

以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!

相關(guān)文章

  • 詳解Alibaba?Java診斷工具Arthas查看Dubbo動(dòng)態(tài)代理類

    詳解Alibaba?Java診斷工具Arthas查看Dubbo動(dòng)態(tài)代理類

    這篇文章主要介紹了Alibaba?Java診斷工具Arthas查看Dubbo動(dòng)態(tài)代理類?,它可以幫助我們查看JDK或者javassist生成的動(dòng)態(tài)代理類,當(dāng)然,它的功能遠(yuǎn)不止此,還可以在生產(chǎn)環(huán)境進(jìn)行診斷,需要的朋友可以參考下
    2022-04-04
  • SpringBoot如何使用Template請(qǐng)求http接口

    SpringBoot如何使用Template請(qǐng)求http接口

    在Spring?Boot中,如果你想要通過(guò)模板(template)的方式連接HTTP服務(wù),并發(fā)送HTTP請(qǐng)求,有幾種不同的方式可以實(shí)現(xiàn),但最直接和常用的方式之一是使用RestTemplate,這篇文章主要介紹了SpringBoot使用Template請(qǐng)求http接口,需要的朋友可以參考下
    2024-08-08
  • Java Swing null絕對(duì)布局的實(shí)現(xiàn)示例

    Java Swing null絕對(duì)布局的實(shí)現(xiàn)示例

    這篇文章主要介紹了Java Swing null絕對(duì)布局的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • SpringBoot與spring security的結(jié)合的示例

    SpringBoot與spring security的結(jié)合的示例

    這篇文章主要介紹了SpringBoot與spring security的結(jié)合的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-03-03
  • SpringBoot與SpringCloud的版本對(duì)應(yīng)關(guān)系解讀

    SpringBoot與SpringCloud的版本對(duì)應(yīng)關(guān)系解讀

    本文介紹了SpringBoot與SpringCloud的版本對(duì)應(yīng)關(guān)系,提供了一個(gè)官方的版本對(duì)應(yīng)表,并給出了個(gè)人的一些經(jīng)驗(yàn)總結(jié)
    2024-12-12
  • 多數(shù)據(jù)源@DS和@Transactional實(shí)戰(zhàn)

    多數(shù)據(jù)源@DS和@Transactional實(shí)戰(zhàn)

    這篇文章主要介紹了多數(shù)據(jù)源@DS和@Transactional實(shí)戰(zhàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Java中的ThreadPoolExecutor線程池詳解

    Java中的ThreadPoolExecutor線程池詳解

    這篇文章主要介紹了Java中的ThreadPoolExecutor線程池詳解,當(dāng)線程池中的線程數(shù)大于 corePoolSize 時(shí),keepAliveTime 為多余的空閑線程等待新任務(wù)的最長(zhǎng)時(shí)間,超過(guò)這個(gè)時(shí)間后多余的線程將被終止,需要的朋友可以參考下
    2023-12-12
  • 使用代碼生成器自定義Entity的部分注解

    使用代碼生成器自定義Entity的部分注解

    這篇文章主要介紹了使用代碼生成器自定義Entity的部分注解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • Jenkins自動(dòng)部署Net Core過(guò)程圖解

    Jenkins自動(dòng)部署Net Core過(guò)程圖解

    這篇文章主要介紹了Jenkins自動(dòng)部署Net Core過(guò)程圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-12-12
  • java使用RSA工具進(jìn)行信息加解密

    java使用RSA工具進(jìn)行信息加解密

    我們?cè)陂_(kāi)發(fā)中需要對(duì)用戶敏感數(shù)據(jù)進(jìn)行加解密,比如密碼等信息,這篇文章主要為大家詳細(xì)介紹了java如何使用RSA工具進(jìn)行信息加解密,感興趣的小伙伴可以了解下
    2023-12-12

最新評(píng)論

磴口县| 河源市| 安庆市| 昌都县| 鹤岗市| 通州市| 莱芜市| 晋城| 巴里| 乌拉特中旗| 凤翔县| 黄平县| 陕西省| 新宾| 永兴县| 横山县| 五莲县| 安多县| 怀安县| 定西市| 买车| 即墨市| 称多县| 申扎县| 仁布县| 蓝田县| 汉川市| 平顶山市| 虞城县| 大荔县| 肇庆市| 西和县| 曲阜市| 彭州市| 罗源县| 沾化县| 五大连池市| 涿州市| 浦江县| 郁南县| 哈密市|