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