java:
File file = new File("F:\\tmp\\taiping\\conf-1.json")
MultipartEntity mpEntity = new MultipartEntity()// 文件传输
ContentBody cbFile = new FileBody(file)
mpEntity.addPart("fileContent", cbFile)
CloseableHttpClient client = HttpClients.createDefault()
HttpPost post = new HttpPost("http://localhost:9999/api/values")
post.setEntity(mpEntity)
try {
CloseableHttpResponse response = client.execute(post)
String result = IOUtils.toString(response.getEntity().getContent())
System.out.println(result)
} catch (Exception e) {
e.printStackTrace()
}
.net http webapi
public HttpResponseMessage Post()
{
var content = Request.Content
var uploadDir = HttpContext.Current.Server.MapPath("~/Upload")
var newFileName = ""
var sp = new MultipartMemoryStreamProvider()
Task.Run(async () =>await Request.Content.ReadAsMultipartAsync(sp)).Wait()
foreach (var item in sp.Contents)
{
if (item.Headers.ContentDisposition.FileName != null)
{
var filename = item.Headers.ContentDisposition.FileName.Replace("\"", "")
newFileName = uploadDir + "\\" + filename
var ms = item.ReadAsStreamAsync().Result
using (var br = new BinaryReader(ms))
{
var data = br.ReadBytes((int)ms.Length)
File.WriteAllBytes(newFileName, data)
}
}
}
var result = new Dictionary<string, string>()
result.Add("result", newFileName)
var resp = Request.CreateResponse(HttpStatusCode.OK, result)
resp.Content.Headers.ContentType = new MediaTypeHeaderValue("text/plain")
return resp
}
import java.io.BufferedReaderimport java.io.File
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.io.IOException
import java.io.InputStream
import java.io.InputStreamReader
import java.net.ServerSocket
import java.net.Socket
/**
*
* 文件名:ServerReceive.java
* 实现功能:作为服务器接收客户端发送的文件
*
* 具体实现过程:
* 1、建立SocketServer,等待客户端的连接
* 2、当有客户端连接的时候,按照双方的约定,这时要读取一行数据
* 其中保存客户端要发送的文件名和文件大小信息
* 3、根据文件名在本地创建文件,并建立好流通信
* 4、循环接收数据包,将数据包写入文件
* 5、当接收数据的长度等于提前文件发过来的文件长度,即表示文件接收完毕,关闭文件
* 6、文件接收工作结束
*
*
* 【注:此代码仅为演示客户端与服务器传送文件使用,
* 每一个数据包之前没有文件协议命令
* 具体的协议传输和文件传出的使用阶段可根据自己程序自行放置】
*
*
* 作者:小菜鸟
* 创建时间:2014-08-19
*
* */
public class ServerReceive {
public static void main(String[] args) {
/**与服务器建立连接的通信句柄*/
ServerSocket ss = null
Socket s = null
/**定义用于在接收后在本地创建的文件对象和文件输出流对象*/
File file = null
FileOutputStream fos = null
/**定义输入流,使用socket的inputStream对数据包进行输入*/
InputStream is = null
/**定义byte数组来作为数据包的存储数据包*/
byte[] buffer = new byte[4096 * 5]
/**用来接收文件发送请求的字符串*/
String comm = null
/**建立socekt通信,等待服务器进行连接*/
try {
ss = new ServerSocket(4004)
s = ss.accept()
} catch (IOException e) {
e.printStackTrace()
}
/**读取一行客户端发送过来的约定信息*/
try {
InputStreamReader isr = new InputStreamReader(s.getInputStream())
BufferedReader br = new BufferedReader(isr)
comm = br.readLine()
} catch (IOException e) {
System.out.println("服务器与客户端断开连接")
}
/**开始解析客户端发送过来的请求命令*/
int index = comm.indexOf("/#")
/**判断协议是否为发送文件的协议*/
String xieyi = comm.substring(0, index)
if(!xieyi.equals("111")){
System.out.println("服务器收到的协议码不正确")
return
}
/**解析出文件的名字和大小*/
comm = comm.substring(index + 2)
index = comm.indexOf("/#")
String filename = comm.substring(0, index).trim()
String filesize = comm.substring(index + 2).trim()
/**创建空文件,用来进行接收文件*/
file = new File(filename)
if(!file.exists()){
try {
file.createNewFile()
} catch (IOException e) {
System.out.println("服务器端创建文件失败")
}
}else{
/**在此也可以询问是否覆盖*/
System.out.println("本路径已存在相同文件,进行覆盖")
}
/**【以上就是客户端代码中写到的服务器的准备部分】*/
/**
* 服务器接收文件的关键代码*/
try {
/**将文件包装到文件输出流对象中*/
fos = new FileOutputStream(file)
long file_size = Long.parseLong(filesize)
is = s.getInputStream()
/**size为每次接收数据包的长度*/
int size = 0
/**count用来记录已接收到文件的长度*/
long count = 0
/**使用while循环接收数据包*/
while(count < file_size){
/**从输入流中读取一个数据包*/
size = is.read(buffer)
/**将刚刚读取的数据包写到本地文件中去*/
fos.write(buffer, 0, size)
fos.flush()
/**将已接收到文件的长度+size*/
count += size
System.out.println("服务器端接收到数据包,大小为" + size)
}
} catch (FileNotFoundException e) {
System.out.println("服务器写文件失败")
} catch (IOException e) {
System.out.println("服务器:客户端断开连接")
}finally{
/**
* 将打开的文件关闭
* 如有需要,也可以在此关闭socket连接
* */
try {
if(fos != null)
fos.close()
} catch (IOException e) {
e.printStackTrace()
}//catch (IOException e)
}//finally
}//public static void main(String[] args)
}//public class ServerReceive
没做过这样的,不过php接收文件的程序你做一个或许有启发<form enctype="multipart/form-data" action="upload.php" method="post">
<input type="hidden" name="max_file_size" value="100000">
<input name="userfile" type="file">
<input type="submit" value="上传文件">
</form>
------------------
upload.php
------------------
<?copy($_FILES[MyFile][tmp_name],$_FILES[MyFile][name])?>
---------------------------
如果这个小程序你用不了,说明你的apache设置有问题,
如果能用就输出
foreach($_FILES[MyFile] as $k=>$v)
echo $k."->".$v."\n<br>"
看看你都得到了什么,找到病因,再做下一步
在线解决,有进展,你改问题充,我改答复
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)