日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

jSignature签名的用法,一文教会你(二)后台代码

發布時間:2025/3/15 编程问答 12 豆豆
生活随笔 收集整理的這篇文章主要介紹了 jSignature签名的用法,一文教会你(二)后台代码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、先在我們的項目里加幾個工具類,代碼如下

AbstractUploadAction (名字可以自取,這個不影響)

import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.io.OutputStream;import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;import org.apache.tomcat.util.http.fileupload.IOUtils; public abstract class AbstractUploadAction {public void get(HttpServletRequest request, HttpServletResponse response, String storeDir) {String requestPath = "get";String requestURI = request.getRequestURI();String path = requestURI.substring(requestURI.indexOf(requestPath) + 3);String separator = File.separator;String lcPath = "";File directory = new File(storeDir);String tempPath = directory.getAbsolutePath() + path.replace("/", separator);File downloadFile = new File(tempPath);if (downloadFile.exists()) {lcPath = directory.getAbsolutePath() + path.replace("/", separator);}try {InputStream input = new FileInputStream(lcPath);OutputStream output = response.getOutputStream();IOUtils.copy(input, output);input.close();output.close();} catch (Exception var14) {}}}

UploadFileAction 這里有一個@Value("${store.dir}") private String storeDir; 這是在yml里面的定義的,這里就是把文件放在我的D盤下的ms_file文件夾下面

import java.io.IOException;import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController;@RestController @RequestMapping("/res/file") public class UploadFileAction extends AbstractUploadAction{@Value("${store.dir}")private String storeDir;//這個是在yml里面定義的/*** * @param folder 文件文件夾。即分類文件夾。如imgType1,imgType2。通過url的upload/后面的值來取值。也可換為其他取值方式。* @param request* @return* @throws IOException*/@RequestMapping(value = {"upload/{folder}"},method = {RequestMethod.POST, RequestMethod.GET})@ResponseBodypublic Object upload(@PathVariable("folder") String folder, HttpServletRequest request) throws IOException {UploadFileUtil uploadFileUtil = new UploadFileUtil();return uploadFileUtil.upload(storeDir, folder, request);}@RequestMapping("/get/**")public void download(HttpServletRequest request, HttpServletResponse response) {this.get(request, response, this.storeDir);} }

UploadUtil

import org.apache.tomcat.util.http.fileupload.IOUtils; import org.springframework.web.multipart.MultipartFile;import java.io.*;public class UploadUtil {public static void copyFile(MultipartFile file, File locFile) throws IOException {InputStream input = file.getInputStream();OutputStream output = new FileOutputStream(locFile);IOUtils.copy(input, output);output.close();input.close();}/**** @param requestUri 這個是數據庫的url,如/res/file/get/signature/20200709114433.jpg* @param fileDir 這個是文件的實際存放地址,如D://ms_file* @return*/public static boolean delete(String requestUri, String fileDir) {String requestPath = "get";String path = requestUri.substring(requestUri.indexOf(requestPath) + 3);//File directory = new File(fileDir);//String tempPath = directory.getAbsolutePath() + path;String tempPath = fileDir + path;File deleteFile = new File(tempPath);return deleteFile.exists() ? deleteFile.delete() : false;}}

Base64ToPicture (這個是base64和圖片互轉的一個工具類)

import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder;import java.io.*; import java.util.Base64; public class Base64ToPicture {/*** 將base64轉成圖片并存到指定文件夾* @param imgStrs* @param imgFilePath* @return*/public static boolean GenerateImage(String imgStrs, String imgFilePath) {// 對字節數組字符串進行Base64解碼并生成圖片if (imgStrs == null){// 圖像數據為空return false;}int imgStrLen = imgStrs.length();String imgStr = imgStrs.substring(22,imgStrLen);//System.out.println(imgStr);BASE64Decoder decoder = new BASE64Decoder();try {// Base64解碼byte[] bytes = decoder.decodeBuffer(imgStr);for (int i = 0; i < bytes.length; ++i) {if (bytes[i] < 0) {// 調整異常數據bytes[i] += 256;}}// 生成jpeg圖片OutputStream out = new FileOutputStream(imgFilePath);out.write(bytes);out.flush();out.close();return true;} catch (Exception e) {return false;}}/*** 將圖片文件轉化為字節數組字符串,并對其進行Base64編碼處理* @param imgFilePath* @return*/public static String GetImageStr(String imgFilePath) {//byte[] data = null;Base64.Decoder decoder = Base64.getDecoder();// 讀取圖片字節數組try {InputStream in = new FileInputStream(imgFilePath);data = new byte[in.available()];in.read(data);in.close();} catch (IOException e) {e.printStackTrace();}// 對字節數組Base64編碼BASE64Encoder encoder = new BASE64Encoder();return encoder.encode(data);// 返回Base64編碼過的字節數組字符串} }

2、看一下我的service吧

public Result save(ProjectFile projectFile, HttpServletRequest request){Result result = new Result();SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");String fileAdd = sdf.format(new Date());//圖片名稱是 當前日期 避免數據重復String fileName = fileAdd + ".jpg";//這個Base64ToPicture就是我們上面的工具類//這個步驟是把base64轉成圖片,并存在storeDir+"/signature/"+fileName這個目錄下,這個storeDir是我們自己定義的,比如這個是D:/ms_file/signature/Base64ToPicture.GenerateImage(projectFile.getEncode(),storeDir+"/signature/"+fileName);//下面的步驟是給我們的數據庫插入一條關于這個簽名的記錄,方便我們以后找到他String url = "/res/file/get/" + "signature" + "/" + fileName;projectFile.setId(UUIDTool.createUUID());projectFile.setUrl(url);projectFile.setCreateTime(new Date());projectFile.setFileType("jpg");projectFile.setType("signature");projectFile.setName(fileName);projectFileMapper.insert(projectFile);result.setMessage("簽名成功!");result.setCode(1);return result;}

這個操作過后我們發現我們的數據庫多了一條記錄,數據庫自己設計,在這里URL我存的是這樣的格式的:/res/file/get/signature/20200709114433.jpg,其實我們的文件在D:\ms_file\signature\20200709114433.jpg,我們會發現這里沒有指定是哪個盤,哪個文件夾下,反而多了/res/file/get/這串東西,大家別著急,這個是我們自己定義的。我們可以發現UploadFileAction 這個工具類里定義了@RequestMapping("/res/file"),get也是在UploadFileAction 里定義的。我們數據庫要和這里保持一致性,大家可以隨便定義;

3、查看我們的簽名

先去數據庫中查到我們需要的那條數據,拿到URL,就也是上面舉例的/res/file/get/signature/20200709114433.jpg。
在頁面中我們這樣顯示

http({data: {projectId:projectId,institutionsId:institutionsId},url: 'projectFile/queryByInstitutionsId',type: 'post',dataType: 'json',success: function(data) {that.signatureList = data;for(var i = 0;i<that.signatureList.length;i++){$("#imageList").append("<img src=http://127.0.0.1:7003" + that.signatureList[i].url + ">");}} });

只需要拼接起來就好了,后臺端口加我們的URL就可以了,它會去調用我們工具類中的get方法,從而顯示出來。

這些工具類還可以用于圖片上傳。這個就放到下期吧!

總結

以上是生活随笔為你收集整理的jSignature签名的用法,一文教会你(二)后台代码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。