WebClient上傳檔案Jsp接受檔案流資料

來源:互聯網
上載者:User

CS軟體給BS端jsp頁面發送資料,jsp接受資料並儲存為檔案。

以下是Csharp發送資料代碼:

using (FileStream fileStream = new FileStream(file.FullName, FileMode.Open, FileAccess.Read))                            {                                long len = fileStream.Length;                                byte[] imgBytes = new byte[fileStream.Length];                                fileStream.Read(imgBytes, 0, (int)fileStream.Length);                                WebClient client = new WebClient();                                string guid = Guid.NewGuid().ToString("N");                                string fileSavePath = GetFileSavePath();                                //路徑格式http://192.168.0.201:7001/affixfile/upload.jsp?newfilename=aaa.jpg&filePath=C:\smz\01\                                string postUrl = string.Format(@"{0}{1}.jpg&filePath={2}\0", AppConfig.GetValue("SavePhotoUrl"), guid, fileSavePath);                                byte[] uploadByte = client.UploadData(postUrl, "POST", imgBytes);}

下面是jsp端接受資料頁面代碼:

<%@ page language="java" contentType="text/html; charset=UTF-8"%> <%@ page import="java.util.*,java.io.*,com.itsv.cms.base.writer.PublishFileWriter;" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html lang="true">  <head>    <title>upload.jsp</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page">  </head>  <%        String fileName = request.getParameter("newfilename");                  String filePath = request.getRealPath("/")+"/uploadImg/"+fileName;        System.out.println("start");         //接收檔案          try {InputStream is=request.getInputStream();filePath=filePath.replaceAll("\\\\", "/");String f=filePath.substring(0,filePath.lastIndexOf("/"));PublishFileWriter.createPath(f);File file=new File(filePath);FileOutputStream fos = new FileOutputStream(file);    int c;    byte b[] = new byte[4*1024];    while ((c=is.read(b))!=-1) {     fos.write(b, 0, c);    }    fos.flush();    is.close();        }catch(Exception e){        e.printStackTrace();        }        System.out.println("end");  %>  <body>    This a struts page. <br>  </body></html>

如果是asp.net頁面接受資料,可採用如下方法:

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.IO;namespace WebCKEditor{    /// <summary>    /// imgUpload 的摘要說明    /// </summary>    public class imgUpload : IHttpHandler    {        public void ProcessRequest(HttpContext context)        {            //context.Response.ContentType = "text/plain";            //context.Response.Write("Hello World");            //App.Log.Info(context.Request.Url.ToString());            //擷取上傳的資料流            string fileNameStr = DateTime.Now.ToString("yyyy-MM-ddHHmmssfff"); //context.Request.QueryString["fileName"];            Stream sr = context.Request.InputStream;            string newfilename = context.Request.QueryString["newfilename"];            try            {                string filename = fileNameStr;                byte[] buffer = new byte[4096];                int bytesRead = 0;                //將當前資料流寫入伺服器端檔案夾ClientBin下                string targetPath = context.Server.MapPath("~/uploadImg/" + newfilename);                using (FileStream fs = File.Create(targetPath, 4096))                {                    while ((bytesRead = sr.Read(buffer, 0, buffer.Length)) > 0)                    {                        //向檔案中寫資訊                        fs.Write(buffer, 0, bytesRead);                    }                }                context.Response.ContentType = "text/plain";                context.Response.Write("上傳成功"+newfilename);            }            catch (Exception e)            {                context.Response.ContentType = "text/plain";                context.Response.Write("上傳失敗, 錯誤資訊:" + e.Message);                //App.Log.Info(e.Message);            }            finally            {                sr.Dispose();            }        }        public bool IsReusable        {            get            {                return false;            }        }    }}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.