天天看點

Winform用戶端到Java服務端的圖檔展示和上傳

版權聲明:歡迎轉載,請注明沉默王二原創。 https://blog.csdn.net/qing_gee/article/details/50425184

圖檔上傳在JavaWeb方面已經有成熟的元件,那麼對應到Winform(C#編寫的富用戶端元件)用戶端到Java服務端的圖檔展示和上傳,其實也有一套為之可行的方案。

一、效果展示

二、方案介紹(展示和上傳)

  1. 展示的時候Java端将圖檔的路徑( http://127.0.0.1:8080/Project/apple.gif ,這也就是說需要一台web伺服器提供http通路,這恰好對應我的項目,但可能并不适合你,請注意)傳遞到C#,C#通過設定

    private System.Windows.Forms.PictureBox

    對象的“ImageLocation”來進行顯示。
  2. 上傳的時候Winform用戶端将選擇的圖檔進行base64編碼,進而通過json字元串傳遞到Java端,然後Java再通過base64進行解碼,完成之後通過ImageIO對象進行圖檔的儲存。

三、圖檔上傳

“上傳”按鈕點選事件:

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;
    }
}           
  1. 點選上傳按鈕時,對彈出的選擇器進行圖檔選擇類型設定。
  2. 通過檔案路徑轉換為Image對象。
  3. 通過ImageToBytes方法對圖檔進行base64編碼(稍候請看詳細代碼)。
  4. 同時擷取圖檔的類型,jpg還是png,供Java端儲存使用。

ImageToBytes方法:

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()會改變MemoryStream的Position,需要重新Seek到Begin
        ms.Seek(0, SeekOrigin.Begin);
        ms.Read(buffer, 0, buffer.Length);
        return Convert.ToBase64String(buffer);
    }
}           

通過ToBase64String方法進行base64的編碼。

這之後,将base64位元組碼和圖檔類型傳遞給Java服務端。

四、圖檔儲存

public static UploadFile saveFile(String image, String type) {
    logger.debug("圖檔内容:" + 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, fileName, 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("圖檔上傳出錯!");
    }
}           
  1. 通過BASE64Decoder的decodeBuffer方法進行位元組碼的解碼操作。
  2. 通過ImageIO對圖檔進行儲存。

相關文章

本文出自:【 沉默王二的部落格