我想通过改装将视频上传到WCF服务。我也可以上传视频,也可以将该视频保存在服务器上的特定路径中。 但是,不幸的是,该视频无法通过任何媒体播放器存储在服务器上后播放。我尝试使用“ WCF”和“ Android Java”这两个代码,如下所示:
在Android中:
@Multipart
@POST("MYSERVER/UploadVideo/")
@Headers({
"Content-Type: multipart/form-data",
"Accept: application/json;charset=utf-8"
})
Call<Verify> uploadVideo(
@Part MultipartBody.Part images
);
RequestBody requestFile =
RequestBody.create(MediaType.parse("multipart/form-data"), file);
fileBody =
MultipartBody.Part.createFormData("videoData", file.getName(), requestFile);
Call<Verify> call = service.uploadVideo(fileBody);
在WCF服务中:
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/UploadVideo/",
RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
VerifyModel UploadVideo(Stream video);
string apPath = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath + "Uploads\\Videos\\" + videoname;
using (FileStream fs = new FileStream(apPath, FileMode.CreateNew, FileAccess.Write))
{
CopyStream(video, fs);
}
public static void CopyStream(Stream input, Stream output)
{
byte[] buffer = new byte[8 * 1024];
int len;
while ((len = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, len);
}
}
正如我告诉您的,我可以将视频保存为正确的大小和mp4格式。但是视频不能在任何媒体播放器上播放
答案 0 :(得分:0)
I hope this will helpful for you. Retrofit with rxJava
@Headers({
"Accept: application/json"
})
@Multipart
@POST("/api/uploadfiles")
Observable<UploadFileModel> uploadData(@Header("Authorization") String
headerKey, @Part("destFolderPath") RequestBody destFolderPath, @Part
MultipartBody.Part file);
/**
*
* @param fileName Name of Uploaded File here
* @param mineType MineType of uploaded File
* @param byteArray i convert the uri to byte array by InputStream you can use
file also
*/
public void getData(String fileName,String mineType , byte byteArray[]) {
//
Log.e("File Name", fileName);
Log.e("MineType", mineType);
String path = ""; // this is destination folder path where i want to upload the file
RequestBody description = RequestBody.create(MultipartBody.FORM, path);
String file_Name = fileName;
// create RequestBody instance from file
RequestBody requestBody = RequestBody.create(MediaType.parse("*/*"), byteArray);
// MultipartBody.Part is used to send also the actual file name
MultipartBody.Part fileToUpload = MultipartBody.Part.createFormData("file", file_Name, requestBody);
AppController appController = AppController.create(mContext);
ApiInterface apiInterface = appController.getUserService();
Disposable disposable = apiInterface.uploadData(sharedPreferences.getString(TOEKNTYPE, "") + " " + sharedPreferences.getString(ACCESS_TOKEN, ""), description, fileToUpload)
.subscribeOn(appController.subscribeScheduler())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<UploadFileModel>() {
@Override
public void accept(UploadFileModel uploadFileModel) {
loading.set(GONE);
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) {
loading.set(GONE);
getActivity(mContext).getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
Toast.makeText(mContext, "Something wents wrong", Toast.LENGTH_SHORT).show();
}
});
compositeDisposable.add(disposable);
}