我在从我的Android应用程序上传doc文件到.NET WCF的问题。我能够发送文件,但它不支持WCF结束。 下面是我的方法上传:
保护无效checkinmethod(字符串RID)抛出异常{
文件SDCardRoot = Environment.getExternalStorageDirectory();
//创建一个新的文件,指定路径和文件名
//这是我们要保存的文件。
档案文件=新的文件(SDCardRoot,摆脱+DOC);
InputStream的时间=新的FileInputStream(文件);
byte []的字节组=新的字节[(INT)file.length()];
INT AB = 0;
做
{
AB = in.read(字节数组,0,bytearray.length);
}而(AB大于0);
InputStream的mystream =新ByteArrayInputStream的(字节组);
InputStreamEntity SE =新InputStreamEntity(mystream,10000);
HttpPost请求=新HttpPost(http://10.66.52.247/tutorwcf/Service.svc/Service/updateMyDoc1);
request.setHeader(接受,应用/ JSON);
request.setHeader(内容型,应用程序/ MSWORD);
request.setEntity(SE);
尝试 {
DefaultHttpClient的HttpClient =新DefaultHttpClient();
HTT presponse响应= httpClient.execute(要求);
HttpEntity responseEntity = response.getEntity();
//读取响应数据到缓冲区
的char []缓冲区=新的char [(INT)responseEntity.getContentLength()];
InputStream的流= responseEntity.getContent();
InputStreamReader的读者=新的InputStreamReader(流);
reader.read(缓冲液);
stream.close();
statuss.setText(新的字符串(缓冲));
//
}
赶上(例外五){
// TODO:处理异常
Log.e(喜,例外,E);
statuss.setText(异常);
}
}
下面是.NET code:
的FileStream fileToupload =新的FileStream(D:\\ myfile.doc,FileMode.Create,FileAccess.Write);
byte []的字节组=新的字节[10000]
INT读取动作,totalBytesRead = 0;
做
{
读取动作= mystream.Read(字节数组,0,bytearray.Length);
totalBytesRead + =读取动作;
}而(读取动作大于0);
fileToupload.Write(字节数组,0,bytearray.Length);
fileToupload.Close();
fileToupload.Dispose();
返回成功;
}
请发送链接或code或任何东西。
如果你没有知道关于这个请排列了这个问题.. 谢谢
解决方案 公共无效checkinstream(RID字符串,字符串文件名)抛出IOException异常
{
网址URL = NULL;
HttpURLConnection的康恩= NULL;
DataOutputStream类DOS = NULL;
inStream中的DataInputStream = NULL;
字符串existingFileName = NULL;
existingFileName =/mnt/sdcard/"+rid+".doc;
INT读取动作,方bytesAvailable,缓冲区大小;
byte []的缓冲区;
INT maxBufferSize = Integer.MAX_VALUE的;
字符串responseFromServer =;
URL =新的URL(http://10.66.51.241/mywcf/Service.svc/Service/uploadMyDoc);
尝试
{
// ------------------客户端请求
的FileInputStream的FileInputStream =新的FileInputStream(新文件(existingFileName));
//打开一个HTTP连接网址
康恩=(HttpURLConnection类)url.openConnection();
//允许输入
conn.setDoInput(真正的);
//允许输出
conn.setDoOutput(真正的);
//不要使用缓存副本。
conn.setUseCaches(假);
//使用POST方法。
conn.setRequestMethod(POST);
conn.setRequestProperty(连接,保持活动);
conn.setRequestProperty(内容类型,应用程序/流);
DOS =新DataOutputStream类(conn.getOutputStream());
// dos.writeBytes(twoHyphens +边界+ lineEnd);
//dos.writeBytes("Content-Disposition:表格数据;名称= \UploadedFile的\;文件名= \+ existingFileName +\+ lineEnd);
// dos.writeBytes(lineEnd);
//创建最大大小的缓冲区
方bytesAvailable = fileInputStream.available();
BUFFERSIZE = Math.min(方bytesAvailable,maxBufferSize);
缓冲区=新的字节[BUFFERSIZE]
//读取文件,并将其写入形式...
读取动作= fileInputStream.read(缓冲液,0,BUFFERSIZE);
而(读取动作大于0)
{
dos.write(缓冲液,0,BUFFERSIZE);
方bytesAvailable = fileInputStream.available();
BUFFERSIZE = Math.min(方bytesAvailable,maxBufferSize);
读取动作= fileInputStream.read(缓冲液,0,BUFFERSIZE);
}
//发送多部分表格数据文件数据后necesssary ...
dos.writeBytes(lineEnd);
//关闭流
Log.e(调试,twoHyphens +边界+ twoHyphens + lineEnd);
fileInputStream.close();
dos.flush();
dos.close();
}
赶上(MalformedURLException的前)
{
Log.e(调试,错误:+ ex.getMessage(),前);
}
赶上(IOException异常IOE)
{
Log.e(调试,错误:+ ioe.getMessage(),IOE);
}
// ------------------读取服务器响应
尝试 {
inStream中=新的DataInputStream(conn.getInputStream());
字符串str;
而((海峡= inStream.readLine())!= NULL)
{
Log.e(调试,服务器响应+ STR);
statuss.setText(STR);
}
inStream.close();
}
赶上(IOException异常ioex){
Log.e(调试,错误:+ ioex.getMessage(),ioex);
}
}
在.NET端创建一个接收流的WCF方法。 谢谢。
I have problem in uploading .doc file to .Net WCF from my Android app. I am able to send file but it is not supported on WCF end. Here is my method for uploading:
protected void checkinmethod(String rid) throws Exception {
File SDCardRoot = Environment.getExternalStorageDirectory();
//create a new file, specifying the path, and the filename
//which we want to save the file as.
File file = new File(SDCardRoot, rid+".doc");
InputStream in = new FileInputStream(file);
byte[] bytearray=new byte[(int) file.length()];
int ab=0;
do
{
ab=in.read(bytearray, 0, bytearray.length);
} while(ab>0);
InputStream mystream= new ByteArrayInputStream(bytearray);
InputStreamEntity se=new InputStreamEntity(mystream, 10000);
HttpPost request = new HttpPost("http://10.66.52.247/tutorwcf/Service.svc/Service/updateMyDoc1");
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/msword");
request.setEntity(se);
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);
HttpEntity responseEntity = response.getEntity();
// Read response data into buffer
char[] buffer = new char[(int)responseEntity.getContentLength()];
InputStream stream = responseEntity.getContent();
InputStreamReader reader = new InputStreamReader(stream);
reader.read(buffer);
stream.close();
statuss.setText(new String(buffer));
//
}
catch (Exception e) {
// TODO: handle exception
Log.e("hi", "exception is", e);
statuss.setText("exception");
}
}
here is .net code:
FileStream fileToupload = new FileStream("D:\\myfile.doc", FileMode.Create, FileAccess.Write);
byte[] bytearray = new byte[10000];
int bytesRead, totalBytesRead = 0;
do
{
bytesRead = mystream.Read(bytearray, 0, bytearray.Length);
totalBytesRead += bytesRead;
} while (bytesRead > 0);
fileToupload.Write(bytearray, 0, bytearray.Length);
fileToupload.Close();
fileToupload.Dispose();
return "success";
}
Please send links or code or any thing.
If you don't have idea about this please rank up this question.. thanks
解决方案public void checkinstream(String rid, String filename ) throws IOException
{
URL url=null;
HttpURLConnection conn = null;
DataOutputStream dos = null;
DataInputStream inStream = null;
String existingFileName= null;
existingFileName= "/mnt/sdcard/"+rid+".doc";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = Integer.MAX_VALUE;
String responseFromServer = "";
url = new URL("http://10.66.51.241/mywcf/Service.svc/Service/uploadMyDoc");
try
{
//------------------ CLIENT REQUEST
FileInputStream fileInputStream = new FileInputStream(new File(existingFileName) );
// Open a HTTP connection to the URL
conn = (HttpURLConnection) url.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", "application/stream");
dos = new DataOutputStream( conn.getOutputStream() );
// dos.writeBytes(twoHyphens + boundary + lineEnd);
//dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + existingFileName + "\"" + lineEnd);
// dos.writeBytes(lineEnd);
// create a buffer of maximum size
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// read file and write it into form...
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);
}
// send multipart form data necesssary after file data...
dos.writeBytes(lineEnd);
// close streams
Log.e("Debug",twoHyphens + boundary + twoHyphens + lineEnd);
fileInputStream.close();
dos.flush();
dos.close();
}
catch (MalformedURLException ex)
{
Log.e("Debug", "error: " + ex.getMessage(), ex);
}
catch (IOException ioe)
{
Log.e("Debug", "error: " + ioe.getMessage(), ioe);
}
//------------------ read the SERVER RESPONSE
try {
inStream = new DataInputStream ( conn.getInputStream() );
String str;
while (( str = inStream.readLine()) != null)
{
Log.e("Debug","Server Response "+str);
statuss.setText(str);
}
inStream.close();
}
catch (IOException ioex){
Log.e("Debug", "error: " + ioex.getMessage(), ioex);
}
}
On the .net end create a wcf method which receives stream. Thanks.