Image Display and uploading from Winform client to Java Server
Image uploading has mature components in JavaWeb, so it corresponds to the image display and uploading from Winform (C # rich client component) client to Java Server, in fact, there is also a set of feasible solutions.
I. effect display
Ii. solution introduction (presentation and upload) when the Java side displays the image path (http: // 127.0.0.1: 8080/Project/apple.gif, that is to say, a web server is required to provide http access, which corresponds to my project, but may not be suitable for you. Please note) pass to C #, C # by setting
private System.Windows.Forms.PictureBox
The "ImageLocation" of the object to display. During the upload, The Winform client base64 encodes the selected image and transmits it to the Java end through a json string. Then, Java decodes the image through base64, and saves the image through the ImageIO object. 3. upload images
"Upload" button click event:
private void btnUpload_Click(object sender, EventArgs e){ openFileDialog1.FileName = string.Empty; openFileDialog1.Filter = "Image Files(JPEG,GIF,BMP,etc.)|" + "*.jpg;*.jpeg;*.gif;*.bmp;*.png|" + "All files(*.*)|*.*"; if (openFileDialog1.ShowDialog() == DialogResult.OK) { string picPath = openFileDialog1.FileName; txtFilePath.Text = picPath; Image image = Image.FromFile(picPath); dealImage = ImageHelper.ImageToBytes(image); dealImageType = System.IO.Path.GetExtension(picPath); picDeal.ImageLocation = picPath; }}
When you click the upload button, set the image selection type for the pop-up selector. Convert to an Image object through the file path. Use the ImageToBytes method to base64 encode the image (please refer to the detailed code later ). At the same time, the image type can be obtained. jpg or. png, which can be saved and used by Java.
ImageToBytes method:
Public static string ImageToBytes (Image image) {ImageFormat format = image. rawFormat; using (MemoryStream MS = new MemoryStream () {if (format. equals (ImageFormat. jpeg) {image. save (MS, ImageFormat. jpeg);} else if (format. equals (ImageFormat. png) {image. save (MS, ImageFormat. png);} else if (format. equals (ImageFormat. bmp) {image. save (MS, ImageFormat. bmp);} else if (format. equals (ImageFormat. gif) {image. save (MS, ImageFormat. gif);} else if (format. equals (ImageFormat. icon) {image. save (MS, ImageFormat. icon);} byte [] buffer = new byte [ms. length]; // Image. save () will change the Position of MemoryStream, and you need to re-Seek to Begin ms. seek (0, SeekOrigin. begin); ms. read (buffer, 0, buffer. length); return Convert. toBase64String (buffer );}}
Use the ToBase64String method to encode base64.
After that, the base64 bytecode and image type are passed to the Java Server.
Iv. Save Images
Public static UploadFile saveFile (String image, String type) {logger. debug ("image Content:" + image); logger. debug ("-------------------------------------"); if (image = null | "". equals (image) {return null;} BASE64Decoder decoder = new BASE64Decoder (); try {String fileName = DateUtil. getCurrentMillStr () + new Random (). nextInt (100) + ". "+ type; UploadFile file = new UploadFile (SAVE_DIRECTORY, fileN Ame, type); byte [] bytes1 = decoder. decodeBuffer (image); ByteArrayInputStream bais = new ByteArrayInputStream (bytes1); BufferedImage bi1 = ImageIO. read (bais); File w2 = file. getFile (); ImageIO. write (bi1, type, w2); return file;} catch (IOException e) {throw new OrderException ("Image Upload error! ");}}
The decodeBuffer method of BASE64Decoder is used to decode bytecode. Save the image through ImageIO.