The app is uploaded to the server via Base64 multiple images and stored in database data

Source: Internet
Author: User
Tags decrypt

Import Java.awt.image.BufferedImage;

Import Java.io.ByteArrayInputStream;

Import Java.io.File;

Import java.io.IOException;

Import Java.util.HashMap;

Import Java.util.Map;


Import Javax.annotation.Resource;

Import Javax.imageio.ImageIO;

Import Javax.servlet.http.HttpServletRequest;

Import Com.shangyu.action.AppBaseController;

Import Org.springframework.stereotype.Controller;

Import org.springframework.web.bind.annotation.RequestMapping;

Import Org.springframework.web.bind.annotation.ResponseBody;

Import Com.shangyu.core.utils.PropertyUtils;

Import com.shangyu.entity.dsz.AppSession;

Import Com.shangyu.service.dsz.PublicImageService;

Import Com.shangyu.service.dsz.WorksummaryService;

Import Com.shangyu.entity.dsz.Worksummary;

Import Com.shangyu.web.DszUtils;

Import Sun.misc.BASE64Decoder;


@Controller

@RequestMapping ("/app/worksummary")

public class Appworksummarycontroller extends appbasecontroller{


Shangyu.properties configuration file picture path:

Windows Storage path------->file.path=c:/nginx/img/dsz/

Windows Read path------->imgurl=http\://115.35.220.220\:8081/dsz/

Linux Storage path--------->file.path=/usr/local/file_root/dsz/

Linux Read path--------->imgurl=http\://182.95.5.128\:8081/dsz/


/**

*

* @param formfile---> Android or iOS pass to the background after Base64 encrypted image stitching string

* @param sessionId

* @param request

* @return

*/

@RequestMapping ("/save.json")

@ResponseBody

Public map<string, object> Save (String sessionId, String formfile, HttpServletRequest request) {

map<string, object> rlt = new hashmap<string, object> ();

map<string, object> param = new hashmap<string, object> ();

try {

Appsession session = Validatesession (SESSIONID);

App not logged in

if (session = = null) {

Rlt.put ("Retcode", "1");

Rlt.put ("message", "not logged in");

return RLT;

}

Worksummary entity = new Worksummary (Session.getsupplierid (), Session.getuserid ());

Worksummaryservice.save (entity);

Param.put ("Bizid", Entity.getuid ());//Log ID

Param.put ("BizType", 2);//Picture library Type--->2 represents the log type

Upload image

if (formfile! = null) {

Uploadimgs (param, formfile);

}

Rlt.put ("Retcode", "0");

Rlt.put ("Message", "add success");

System.out.println (Param.get ("Code"));

return RLT;

} catch (Exception e) {

Rlt.put ("Retcode", "2");

Rlt.put ("Message", "System exception," + E.getmessage ());

return RLT;

}

}


/**

* @param formfile

*

* @Title: Uploadimgs

* @Description: Upload images

* @param map

* @param formfile

* @param Bizid

* @param biztype

* @return void return type

* @throws

*/


public void Uploadimgs (map<string, object> param, String formfile) {


try {

When you edit a page in the app log, all the original pictures corresponding to that log are deleted based on the log ID and picture type.

list<publicimage> imglist = Publicimageservice.findbymap (param);

if (imglist.size () > 0) {

for (int j = 0; J < Imglist.size (); j + +) {

Publicimageservice.delete (Imglist.get (j). Getbizid ());

}

}

Split multiple pictures to traverse

String[] List=formfile.split (",");

for (int i = 0; i < list.length; i++) {

Read the absolute path to the image where the configuration file is stored (the location of the uploaded image is the disk directory)

String Imgpath = Propertyutils.getpropertyvalue ("Shangyu.properties", "File.path") + "imgs/";

Create a picture path (directory)

File TargetDir = new file (Imgpath);

if (!targetdir.exists ()) {

Targetdir.mkdirs ();

}

Create a new picture name (and format)

String filename = dszutils.uuid () + ". jpg";

File NewFile = new file (imgpath + filename);

Base64decoder decoder = new Base64decoder ();

Decrypt the image data into a byte array via Base64 decryption

byte[] bytes = Decoder.decodebuffer (List[i]);//Decrypt each picture

Constructing a byte array input stream

Bytearrayinputstream Bais = new Bytearrayinputstream (bytes);

Reading data from an input stream

BufferedImage bi = imageio.read (Bais);

Write data information into a picture file

Imageio.write (BI, "JPG", newfile);//regardless of the output format picture, here do not need to change

Bais.close ();

Picture name in IMGs directory (picture relative path)

String img = "imgs/" + filename;

Add all pictures submitted by the Journal to the photo gallery (save picture)

Publicimage entity = new Publicimage ();

Entity.setbiztype ((Integer) (Param.get ("BizType"));//Picture type

Entity.setbizid (String) param.get ("Bizid");//log primary key ID associated with the picture library

Entity.setpath (IMG);//Picture relative path

Entity.setimgname (filename);//Picture name

Publicimageservice.save (entity);

Param.put ("Code", "Success");

}

} catch (Exception e) {

E.printstacktrace ();

Param.put ("Code", "Picture upload exception," +e.getmessage ());

}

}


}




Case study: Android uploads images to the server via Base64

This article from the "extraordinary life-seekers of knowledge" blog, declined to reprint!

The app is uploaded to the server via Base64 multiple images and stored in database data

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.