之前在做一个小项目,C#的,最后需要生成.mid的MIDI文件。网上找了很多资料,大部分都是读取,没有写入。嗯我承认其实读取反过来就是写入。但是没有封装好的类好麻烦的。。
突然看到了MidiShow的在线编辑功能,眼前一亮,想到了用来写网页的PHP。于是我就百度了,找到了一个类库。项目里模拟http请求去调用它。现在放出来,一是以后备查,二是也希望方便大家。
API说明及测试页:https://player.pocketcraft.bid/midi/api/
服务器弱,经不住攻击,各位手下留情。
以下是从互联网上找到的关于POST上传文件的代码,希望能对你有所启发
C# 摘自:http://www.cnblogs.com/password1/p/5870725.html
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Net;
namespace HuaTong.General.Utility
{
/// <summary>
/// 用于模拟POST上传文件到服务器
/// </summary>
public class HttpUpload
{
private ArrayList bytesArray;
private Encoding encoding = Encoding.UTF8;
private string boundary = String.Empty;
public HttpUpload()
{
bytesArray = new ArrayList();
string flag = DateTime.Now.Ticks.ToString("x");
boundary = "---------------------------" + flag;
}
/// <summary>
/// 合并请求数据
/// </summary>
/// <returns></returns>
private byte[] MergeContent()
{
int length = 0;
int readLength = 0;
string endBoundary = "--" + boundary + "--\r\n";
byte[] endBoundaryBytes = encoding.GetBytes(endBoundary);
bytesArray.Add(endBoundaryBytes);
foreach (byte[] b in bytesArray)
{
length += b.Length;
}
byte[] bytes = new byte[length];
foreach (byte[] b in bytesArray)
{
b.CopyTo(bytes, readLength);
readLength += b.Length;
}
return bytes;
}
/// <summary>
/// 上传
/// </summary>
/// <param name="requestUrl">请求url</param>
/// <param name="responseText">响应</param>
/// <returns></returns>
public bool Upload(String requestUrl, out String responseText)
{
WebClient webClient = new WebClient();
webClient.Headers.Add("Content-Type", "multipart/form-data; boundary=" + boundary);
byte[] responseBytes;
byte[] bytes = MergeContent();
try
{
responseBytes = webClient.UploadData(requestUrl, bytes);
responseText = System.Text.Encoding.UTF8.GetString(responseBytes);
return true;
}
catch (WebException ex)
{
Stream responseStream = ex.Response.GetResponseStream();
responseBytes = new byte[ex.Response.ContentLength];
responseStream.Read(responseBytes, 0, responseBytes.Length);
}
responseText = System.Text.Encoding.UTF8.GetString(responseBytes);
return false;
}
/// <summary>
/// 设置表单数据字段
/// </summary>
/// <param name="fieldName">字段名</param>
/// <param name="fieldValue">字段值</param>
/// <returns></returns>
public void SetFieldValue(String fieldName, String fieldValue)
{
string httpRow = "--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}\r\n";
string httpRowData = String.Format(httpRow, fieldName, fieldValue);
bytesArray.Add(encoding.GetBytes(httpRowData));
}
/// <summary>
/// 设置表单文件数据
/// </summary>
/// <param name="fieldName">字段名</param>
/// <param name="filename">字段值</param>
/// <param name="contentType">内容内型</param>
/// <param name="fileBytes">文件字节流</param>
/// <returns></returns>
public void SetFieldValue(String fieldName, String filename, String contentType, Byte[] fileBytes)
{
string end = "\r\n";
string httpRow = "--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
string httpRowData = String.Format(httpRow, fieldName, filename, contentType);
byte[] headerBytes = encoding.GetBytes(httpRowData);
byte[] endBytes = encoding.GetBytes(end);
byte[] fileDataBytes = new byte[headerBytes.Length + fileBytes.Length + endBytes.Length];
headerBytes.CopyTo(fileDataBytes, 0);
fileBytes.CopyTo(fileDataBytes, headerBytes.Length);
endBytes.CopyTo(fileDataBytes, headerBytes.Length + fileBytes.Length);
bytesArray.Add(fileDataBytes);
}
}
}
PHP 摘自:http://www.cnblogs.com/jackluo/p/4113255.html
<?php
/**
* Email net.webjoy@gmail.com
* author jackluo
* 2014.11.21
*
*/
//*
function curl_post($url, $data, $header = array()){
if(function_exists('curl_init')) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
if(is_array($header) && !empty($header)){
$set_head = array();
foreach ($header as $k=>$v){
$set_head[] = "$k:$v";
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $set_head);
}
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);// 1s to timeout.
$response = curl_exec($ch);
if(curl_errno($ch)){
//error
return curl_error($ch);
}
$reslut = curl_getinfo($ch);
print_r($reslut);
curl_close($ch);
$info = array();
if($response){
$info = json_decode($response, true);
}
return $info;
} else {
throw new Exception('Do not support CURL function.');
}
}
//*/
//
function api_notice_increment($url, $data)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER,0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
// $data = http_build_query($data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
//curl_file_create
// $result = curl_exec($ch);
$lst['rst'] = curl_exec($ch);
$lst['info'] = curl_getinfo($ch);
curl_close($ch);
return $lst;
// return $result;
}
/**
* curl文件上传
* @var struing $r_file 上传文件的路劲和文件名
*
*/
/*
function upload_file($url,$r_file)
{
$file = array("fax_file"=>'@'.$r_file,'type'=>'image/jpeg');//文件路径,前面要加@,表明是文件上传.
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,$url);
curl_setopt($curl,CURLOPT_POST,1);
curl_setopt($curl,CURLOPT_POSTFIELDS,$file);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, 0);
$result = curl_exec($curl); //$result 获取页面信息
curl_close($curl);
echo $result ; //输出 页面结果
}*/
function upload_file($url,$filename,$path,$type){
$data = array(
'pic'=>'@'.realpath($path).";type=".$type.";filename=".$filename
);
$ch = curl_init();
//设置帐号和帐号名
curl_setopt($ch, CURLOPT_USERPWD, 'joe:secret' );
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true );
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// curl_getinfo($ch);
$return_data = curl_exec($ch);
curl_close($ch);
echo $return_data;
}
// php 5.5 以后请用以下函数
function upload_file($url,$filename,$path,$type){
$data = array(
'pic'=>new CURLFile(realpath($path))
);
$ch = curl_init();
//也可以用以下注释掉的不用改代码,觉得新版的可以省下点代码,看个人
//curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
//设置帐号和帐号名
curl_setopt($ch, CURLOPT_USERPWD, 'joe:secret' );
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true );
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// curl_getinfo($ch);
$return_data = curl_exec($ch);
curl_close($ch);
echo $return_data;
}
if ($_POST) {
$url = 'http://platform.com/upload/image';
//
$path = $_SERVER['DOCUMENT_ROOT'];
/*
print_r($_FILES);
exit;
*/
//$filename = $path."/232.jpg";
//upload tmp
$tmpname = $_FILES['fname']['name'];
$tmpfile = $_FILES['fname']['tmp_name'];
$tmpType = $_FILES['fname']['type'];
// echo $tmpType;
upload_file($url,$tmpname,$tmpfile,$tmpType);
/*
$data = array(
'path'=>"@$path/232.jpg",
'name'=>'h'
);
*/
//'pic'=>'@/tmp/tmp.jpg', 'filename'=>'tmp'
//$data = array('pic'=>"@$filename", 'filename'=>'tmp');
/*
$data = array(
'uid' => 10086,
'pic' => '@$tmpfile'.';type='.$tmpType
);
$info = api_notice_increment($url, $data);
*/
//$info = curl_post($url, $data);
//$info = api_notice_increment($url, $data);
//upload_file($url,$tmpfile);
//print_r($info);
exit;
/*
$file = 'H:\www\test\psuCARGLSPA-pola.jpg'; //要上传的文件
$src = upload_curl_pic($file);
echo $src;
*/
}
?>
<form action="http://localhost/upload.php" enctype="multipart/form-data" method="post">
<p>UpLoad: <input type="text" name="fname" /></p>
<p>UpLoad: <input type="file" name="fname" /></p>
<input type="submit" value="Submit" />
</form>
JAVA 请看:http://www.cnblogs.com/zdz8207/p/java-httpclient-file.html
更多语言自行百度,谢谢