今天就跟大家聊聊有關(guān)利用ssm框架如何實(shí)現(xiàn)將上傳的圖片保存到本地?cái)?shù)據(jù)庫(kù)中,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
我們注重客戶提出的每個(gè)要求,我們充分考慮每一個(gè)細(xì)節(jié),我們積極的做好成都網(wǎng)站制作、成都做網(wǎng)站服務(wù),我們努力開拓更好的視野,通過不懈的努力,創(chuàng)新互聯(lián)贏得了業(yè)內(nèi)的良好聲譽(yù),這一切,也不斷的激勵(lì)著我們更好的服務(wù)客戶。 主要業(yè)務(wù):網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)站設(shè)計(jì),成都微信小程序,網(wǎng)站開發(fā),技術(shù)開發(fā)實(shí)力,DIV+CSS,PHP及ASP,ASP.Net,SQL數(shù)據(jù)庫(kù)的技術(shù)開發(fā)工程師。
1、前臺(tái)部分
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Title</title> <script src="resources/jquery/jquery-3.0.0.js"></script> </head> <body> <img id="image"src=""/> <br/> <input type="file"onchange="selectImage(this);"/> <br/> <input type="button"onclick="uploadImage();"value="提交"/> <script> var image = ''; function selectImage(file){ if(!file.files || !file.files[0]){ return; } var reader = new FileReader(); reader.onload = function(evt){ document.getElementById('image').src = evt.target.result; image = evt.target.result; } reader.readAsDataURL(file.files[0]); } function uploadImage(){ image = JSON.stringify(image) $.ajax({ type:'POST', url: '/blog/test', data: {base64: image }, async: false, dataType: 'json', success: function(data){ alert(data.success) if(data.success){ alert('上傳成功'); }else{ alert('上傳失敗'); } }, error: function(err){ alert('網(wǎng)絡(luò)故障'); } }); } </script> <script src="jquery-1.11.1.min.js"></script> </body> </html>
2、controller
@Inject private IUserService userService; @RequestMapping(value="test") @ResponseBody public ConsoleResult test(String base64){ // 自定義返回前臺(tái)數(shù)據(jù)格式 ConsoleResult res = new ConsoleResult(); // 去掉base64數(shù)據(jù)頭部data:image/png;base64,和尾部的” " “ String[] ww= base64.split(","); base64 = ww[1]; String[] aa = base64.split("\""); base64 = aa[0]; try { // 將圖片插入數(shù)據(jù)庫(kù) userService.base64test(base64); // 圖片保存到本地 String path = "D:/asdfasdf.jpg"; Base64File file = new Base64File(); file.decoderBase64File(base64, path); // 成功標(biāo)識(shí) res.setStatus(ConsoleResult.successStatus); } catch (Exception e) { res.setStatus(ConsoleResult.faultStatus); } return res; }
3、base64
/** * 將base64字符解碼保存文件 * * @param base64Code * @param targetPath * @throws Exception */ public static void decoderBase64File(String base64Code, String targetPath) { byte[] buffer; FileOutputStream out = null; try { buffer = new BASE64Decoder().decodeBuffer(base64Code); out = new FileOutputStream(targetPath); out.write(buffer); } catch (IOException e) { e.printStackTrace(); } finally { try { if (out != null) { out.close(); } } catch (IOException e) { e.printStackTrace(); } } }
4、mapper.xml
<update id="base64Test" parameterType="String"> update t_user set U_ABOUT = #{base64} where u_name = '971171444' </update>
看完上述內(nèi)容,你們對(duì)利用ssm框架如何實(shí)現(xiàn)將上傳的圖片保存到本地?cái)?shù)據(jù)庫(kù)中有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
網(wǎng)站標(biāo)題:利用ssm框架如何實(shí)現(xiàn)將上傳的圖片保存到本地?cái)?shù)據(jù)庫(kù)中
轉(zhuǎn)載源于:http://aaarwkj.com/article16/gooddg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、定制網(wǎng)站、軟件開發(fā)、品牌網(wǎng)站制作、服務(wù)器托管、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)