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; } } }}