- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试将文件 fmarom android 上传到 Web 服务器,在服务器端使用 aspx,我正在使用我在以下位置找到的示例代码:http://www.codicode.com/art/upload_files_from_android_to_a_w.aspx我的问题是我收到此错误消息:java.net.ProtocolException:方法不支持请求正文:POST。
现在我正在寻找这个错误消息,我发现我在服务器端有问题,我的问题的解决方案是在服务器上同时启用 HTTP GET 和 HTTP POST,通过像这样编辑 Web.config。
<configuration>
<system.web>
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
</system.web>
</configuration>
但仍然有相同的错误消息,在这一行:
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
这是我的类(class)代码:
HttpFileUpload.java:
package com.batyalon.tizko;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.util.Log;
public class HttpFileUpload implements Runnable{
URL connectURL;
String responseString;
String Title;
String Description;
byte[ ] dataToServer;
FileInputStream fileInputStream = null;
HttpFileUpload(String urlString, String vTitle, String vDesc){
try{
connectURL = new URL(urlString);
Title= vTitle;
Description = vDesc;
}catch(Exception ex){
Log.i("HttpFileUpload","URL Malformatted");
}
}
void Send_Now(FileInputStream fStream){
fileInputStream = fStream;
Sending();
}
void Sending(){
String iFileName = "ovicam_temp_vid.mp4";
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
String Tag="fSnd";
try
{
Log.e(Tag,"Starting Http File Sending to URL");
// Open a HTTP connection to the URL
HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();
// Allow Inputs
conn.setDoInput(true);
// Allow Outputs
conn.setDoOutput(true);
// Don't use a cached copy.
conn.setUseCaches(false);
// Use a post method.
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
//The line which gives the error:
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"title\""+ lineEnd);
dos.writeBytes(lineEnd);
dos.writeBytes(Title);
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"description\""+ lineEnd);
dos.writeBytes(lineEnd);
dos.writeBytes(Description);
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + iFileName +"\"" + lineEnd);
dos.writeBytes(lineEnd);
Log.e(Tag,"Headers are written");
// create a buffer of maximum size
int bytesAvailable = fileInputStream.available();
int maxBufferSize = 1024;
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
byte[ ] buffer = new byte[bufferSize];
// read file and write it into form...
int bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0)
{
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable,maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0,bufferSize);
}
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// close streams
fileInputStream.close();
dos.flush();
Log.e(Tag,"File Sent, Response: "+String.valueOf(conn.getResponseCode()));
InputStream is = conn.getInputStream();
// retrieve the response from server
int ch;
StringBuffer b =new StringBuffer();
while( ( ch = is.read() ) != -1 ){ b.append( (char)ch ); }
String s=b.toString();
Log.i("Response",s);
dos.close();
}
catch (MalformedURLException ex)
{
Log.e(Tag, "URL error: " + ex.getMessage(), ex);
}
catch (IOException ioe)
{
Log.e(Tag, "IO error: " + ioe.getMessage(), ioe);
}
}
@Override
public void run() {
// TODO Auto-generated method stub
}
}
我的 MainActivity 代码:
try {
// Set your file path here
FileInputStream fstrm = new FileInputStream(Environment.getExternalStorageDirectory().toString()+ File.separator + "bina/shruttech/3_0.png");
// Set your server page url (and the file title/description)
HttpFileUpload hfu = new HttpFileUpload("http://XXXX:8080/fileup.aspx", "mytitle","mydescription");
hfu.Send_Now(fstrm);
} catch (FileNotFoundException e) {
// Error: File not found
}
我的 fileup.aspx 代码:
protected void Page_Init(object sender, EventArgs e)
{
string vTitle = "";
string vDesc = "";
string FilePath = Server.MapPath("/files/cur_file.mp4");
if (!string.IsNullOrEmpty(Request.Form["title"]))
{
vTitle = Request.Form["title"];
}
if (!string.IsNullOrEmpty(Request.Form["description"]))
{
vDesc = Request.Form["description"];
}
HttpFileCollection MyFileCollection = Request.Files;
if (MyFileCollection.Count > 0)
{
// Save the File
MyFileCollection[0].SaveAs(FilePath);
}
}
在此先感谢您的帮助...
最佳答案
尝试这种方式:upload-binary-data-http-post
关于java - 将图像文件从 android 上传到 aspx 网络服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19235446/
在 ASP.NET 编程的“代码隐藏”风格中,您将代码拆分为两个不同的文件:一个 .aspx 文件和一个 aspx.cs 文件。 (这适用于 C#;对于 VB,则为 aspx.vb。)每个人似乎都决定
在这个项目中你可以看到aspx文件下的aspx.designer.vb和aspx.vb 但是在我的项目中我没有它?? ,, 是什么赋予了?? 我可以通过右键单击然后单击查看代码来查看 vb 代码。 如
我将 my.aspx.cs 文件中的变量值传递到我的 .aspx 文件。当它在 aspx 中时,它的值会发生变化,然后需要返回到 .aspx.cs 文件。但是,它不允许我更改变量的值。有什么建议么?
我在 aspx 页面中使用多个 aspx 用户控件实例时遇到问题。当我尝试通过 Java 脚本获取用户控件元素值时会发生这种情况。 用户控制代码: function ucFun()
我有一个 asp.net 网站,我想知道是否可以将 .aspx 文件调用到另一个 .aspx 文件中。场景是我有 3 个左侧菜单,它们显示在各个页面上。 一开始我只有一个左侧菜单,因此将其添加到我的
我正在使用 C# 和 .NET ASP Web 应用程序开发我的第一个项目。我已成功连接到 SQL 数据库,但如何将输入从 .aspx(html 中)传递到 .aspx.cs(C# 中)? IE。(.
我已经阅读了很多关于同一问题的类似帖子,但这些修复对我不起作用。 我在页面代码的类区域中将变量声明为 Public,并在 Page_Load 子例程中为其分配一个值,但是当我在 javascript
在重命名不同的 ASPX 页面后,我曾多次遇到同样的问题,令我惊讶的是我在 stackoverflow 上找不到遇到同样问题的其他人。 当我运行我的 ASP.NET C# 项目时,调试器给我这样一条消
Select Zone East West North
这是我的 javascript 函数,其中值来自其他页面并且完美接收,但是如何在 cs 页面中检索“divConversation”值。这是我的代码 function myLoad() {
我不是编程新手,而是 Visual Studio、MVC、C#、ASP.NET 和 EXT.NET,即我尝试使用的所有工具的新手。 我正在尝试创建一个 Ext.Net 2.0 MVC4 项目,并获得了
我有一个 HTML 页面和一个 iframe(aspx 页面),如下所示。 Mypage.htm 此 Mypage.htm 将作为来自其他页面的链接提供,用户单击这些页面,Mypage.htm
我们最近设置了一个新的 SSRS 环境,报表的报表管理器 URL 现在为 Reports/Pages/Report.aspx 以前的环境是ReportList/Pages/ReportViewer.a
我正在尝试点击按钮,但我得到了 'ASP.views_home_index_aspx' does not contain a definition for'btnCreateOrder_Click'
我正在尝试将数据从 aspx.vb 页面传递到 aspx 页面。 在 aspx.vb 页面,我在 textbox 中放置了一个值(0 或 1),即 runat="server" 当加载 aspx 页面
我是 asp.net 的新手,正在处理一个有两个 .aspx 页面的问题集。 事件.aspx: 这是网站管理员在已经可用的面板(如事件标题、日期、信息等)的帮助下更新即将举行的事件和网络研讨会的地方。
这是我在 ASP.NET 中尝试执行的操作: 创建一个名为 Main.aspx 的页面。这个页面有一个 DIV 和一个按钮。 浏览器加载 Main.aspx。然后,当我单击该按钮时,我想将页面 Pag
我已经从一个不再可用的人那里继承了一个网站。在已部署的文件夹中,我有 Config.aspx(请参阅代码)。但是我找不到代码隐藏文件。配置页面有效。我在哪里可以找到 .cs 文件? 谢谢
我使用 iframe 来显示一个 aspx 页面,如下所示 this link 在 Displaypdf.aspx.cs 中,我正在显示一个 pdf,如下所示。我在
我的元素中有 2 个 aspx 文件。 first.aspx 页面上有一些内容,当我单击一个按钮时,它将在同一页面上启动一个框架(second.aspx 只有显示日历的代码)。 现在,一旦该日历 (s
我是一名优秀的程序员,十分优秀!