在写APP接口的时候,传上来的图片格式是BASE64,(不知道有没有其他的方式),就只能用base64来存取图片。
第一种方式是存取相关的图片到本地文件夹中:
直接用base64的字符串进行存取。
public String uploadPicture(String photo) {String path="";if( photo!= null && !"".equals(photo)){//获取图片上传地址Properties properties = new Properties();try {properties.Class().getResourceAsStream("/filepath.properties"));} catch (IOException e1) {e1.printStackTrace();}String rootPath = getClass().getResource("/").getFile().toString().replaceAll("%20", " ");rootPath = rootPath.substring(0, rootPath.indexOf("/WEB-INF"));//String path1 = rootPathProperty("image2.url");//图片上传处理Map<String,byte []> photoMap = new HashMap<String,byte []>();try {photoMap = structImageData(photo);//UploadImageUtil.uploadImage(photoMap,path1);//上传图片到制定的文件夹中。配置文件中image.address 的位置 UploadImageUtil.uploadImage(Property("image.address"));} catch (IOException e1) {e1.printStackTrace();}path = ImageNames(photoMap);}return path;}
类文件的写法:
UploadImageUtil
package com.lingnet.util;import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;/*** 图片上传工具类* @ClassName: UploadImageUtil * @author* @date 2015-1-9 上午11:58:32 */
public class UploadImageUtil {/*** @Title: uploadImage * @param map 是key为图片名,value为图片字节数组格式的map* @param destPath 是图片保存在本地硬盤的路徑,如* @throws IOException * @since 2015-1-9 V 1.0*/public static void uploadImage(Map<String,byte []> map, String destPath) throws IOException {String fileName ;//文件名byte [] buf ;//读写图片数据FileOutputStream fos = null;BufferedOutputStream bos = null;//循环读取文件并上传for ( Entry<String, byte[]> entry : Set() ){fileName = Key();buf = Value();File destDir = new File(destPath);if (!ists())destDir.mkdirs();// 保存图片到指定路径File file = new File(destDir, fileName);fos = new FileOutputStream(file);bos = new BufferedOutputStream(fos);bos.write(buf);//bos.flush();bos.close();/* Image src = ad(file);int wideth = Width(null);int height = Height(null);BufferedImage image = new BufferedImage(wideth, height,BufferedImage.TYPE_INT_RGB);Graphics g = ateGraphics();g.drawImage(src, 0, 0, wideth, height, null);//水印文件File _filebiao = new File("D:img.png");Image src_biao = ad(_filebiao);int wideth_biao = Width(null);int height_biao = Height(null);g.drawImage(src_biao, (wideth - wideth_biao)+5,(height - height_biao)+5, wideth_biao, height_biao, null);//水印文件结束g.dispose();FileOutputStream out = new FileOutputStream(file);JPEGImageEncoder encoder = ateJPEGEncoder(out);de(image);out.close();*/}}/*** 将用户提交上来的图片信息构造成可以存入数据库和本地硬盘的有效信息* @Title: constructImageData * @param pictures* @return key为图片名,value为经过Base64Decoder处理的图片字节数组格式的map* @throws IOException * String[] * @since 2015-1-9 V 1.0*/public static Map<String,byte []> constructImageData(String pictures) throws IOException {//List<Map<String,byte []>> list_picture = new ArrayList<Map<String,byte []>>();Map<String,byte []> map_picture = new HashMap<String,byte []>();//先解析上传的图片picturesString [] images = pictures.split(",");for (int i = 0 ; i <images.length;i++){if(images[i]!=null&&!"".equals(images[i])&&!"null".equals(images[i])){String image = images[i];//map_picture = new HashMap<String,byte []>();//构造key为图片名,value为图片字节数组格式的mapSimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");String dateStr = sdf.format(new Date());Random random = new Random();dateStr +Int(4);map_picture.put("imag"+dateStr+".jpg",ansferImageStringToByteArray(image));//放入集合//list_picture.add(map_picture);}}return map_picture;}/*** 获取所有图片的名称* @Title: getImageNames * @param map* @return String * @since 2015-1-9 V 1.0*/public static String getImageNames(Map<String,byte []> map){String names = "";for ( String str : map.keySet() ) {names += "".equals(names)?str:","+str;}return names;}/*** 删除图片* @Title: deleteFile * @param wholeFilePath * void * @author 李志新* @since 2015-1-12 V 1.0*/public static Boolean deleteFile(String wholeFilePath){File file = new File (wholeFilePath);if (ists()){file.delete();return true;}else{return false;}}/*** 复制图片* @Title: copy * @param fileFrom* @param fileTo* @throws IOException * void * @since 2015-2-2 V 1.0*/public static void copy(String fileFrom, String fileTo) throws IOException{FileInputStream fis = new FileInputStream(fileFrom);BufferedInputStream bis = new BufferedInputStream(fis);FileOutputStream fos = new FileOutputStream(fileTo);BufferedOutputStream bos = new BufferedOutputStream(fos);int d = -1;while ((d = ad()) != -1){bos.write(d);}bos.close();bis.close();}}
配置文件的写法:
filepath.properties:
image.address=E:/picture
这种方法比较简单,可以存到制定的文件夹中,读取方法可以参考之前写的文章用来读取。
第二种、因为是客户需求,要把文件和图片等存到相关的服务器共享文件夹中,所以需要用到SMB来处理
public static String imgUpload2(Object image,String path)throws Exception{path = getPropert("filepath.properties", "image.address");String img = "";if(image!=null&&!"".equals(image)){img = String();}if (!path.startsWith("smb")) {Map<String, byte[]> picMap = structImageData(img);// 保存图片UploadImageUtil.uploadImage(picMap, path);
// String imgs[] = ImageNames(picMap).split(",");path = ImageNames(picMap);return path;}else{//将base图片转成可以直接存储的信息Map<String, byte[]> picMap = structImageData(img);//base64转成MultipartFile 进行操作//MultipartFile imageMultipart = String());String res = uploadPic(picMap);return res;}}
其中uploadPicd的方法中写了SMB的用法:
public static String uploadPic(Map<String, byte[]> files) {InputStream in = null;OutputStream out = null;String trueFileName = "";try {String path = getPropert("filepath.properties", "image.address");// 远程服务器共享文件夹名称//String fileName = OriginalFilename();for ( Entry<String, byte[]> entry : Set() ){String fileName = Key();InputStream sbs = new (fileName)); trueFileName = String.valueOf(System.currentTimeMillis()) + fileName;SmbFile remoteFile = new SmbFile(path + trueFileName);t();in = new BufferedInputStream(sbs);out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));byte []buffer = new byte[1024]; while((in.read(buffer)) != -1){ out.write(buffer); buffer = new byte[1024]; } }} catch (Exception e) {String msg = "发生错误:" + e.getLocalizedMessage();System.out.println(msg);} finally {try {if (out != null) {out.close();}if (in != null) {in.close();}} catch (Exception e) {}}return trueFileName;}
这样就吧图片存到共享文件夹中了。
备注:SMB地址的写法 : smb://{username}:{pwd}@{ip}/{文件夹名称}
举例: smb://administrator:123456@172.16.10.136/smb
jar 包: jcifs-1.3.15.jar(网上有很多)
本文发布于:2024-01-31 10:18:41,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170666752727812.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |