我知道这个问题似乎有点具体,但我原以为这是一个很常见的场景。我的目标是将图像(~8 mb)上传到服务器。服务器运行 .NET(我相信没有 php),但我不仅需要上传文件,还需要传递一些文本信息。我该怎么做?我的客户端和服务器代码会是什么样子?
问问题
1185 次
1 回答
1
我使用.Net
WebService 上传Database
文件。同样,您可以将其用于Image
上传。
下载这个演示。
在该包中添加这些文件。
1. OutputStream.java
package com.abhan.example;
public class OutputStream extends java.io.FilterOutputStream {
public final static int ENCODE = 1;
public final static int DO_BREAK_LINES = 8;
private final static int MAX_LINE_LENGTH = 76;
private final static byte NEW_LINE = (byte)'\n';
private final static byte WHITE_SPACE_ENC = -5;
private boolean encode;
private int position;
private byte[] buffer;
private int bufferLength;
private int lineLength;
private boolean breakLines;
private byte[] b4;
private boolean suspendEncoding;
private int options;
private byte[] decodabet;
public OutputStream( java.io.OutputStream out ) {
this( out, ENCODE );
}
public OutputStream( java.io.OutputStream out, int options ) {
super( out );
this.breakLines = (options & DO_BREAK_LINES) != 0;
this.encode = (options & ENCODE) != 0;
this.bufferLength = encode ? 3 : 4;
this.buffer = new byte[ bufferLength ];
this.position = 0;
this.lineLength = 0;
this.suspendEncoding = false;
this.b4 = new byte[4];
this.options = options;
this.decodabet = Base64.getDecodabet(options);
}
@Override
public void write(int theByte)
throws java.io.IOException {
if( suspendEncoding ) {
this.out.write( theByte );
return;
}
if( encode ) {
buffer[ position++ ] = (byte)theByte;
if( position >= bufferLength ) {
this.out.write( Base64.encode3to4( b4, buffer, bufferLength, options ) );
lineLength += 4;
if( breakLines && lineLength >= MAX_LINE_LENGTH ) {
this.out.write( NEW_LINE );
lineLength = 0;
}
position = 0;
}
} else {
if( decodabet[ theByte & 0x7f ] > WHITE_SPACE_ENC ) {
buffer[ position++ ] = (byte)theByte;
if( position >= bufferLength ) {
int len = Base64.decode4to3( buffer, 0, b4, 0, options );
out.write( b4, 0, len );
position = 0;
}
}
else if( decodabet[ theByte & 0x7f ] != WHITE_SPACE_ENC ) {
throw new java.io.IOException( "Invalid character in Base64 data." );
}
}
}
@Override
public void write( byte[] theBytes, int off, int len )
throws java.io.IOException {
if( suspendEncoding ) {
this.out.write( theBytes, off, len );
return;
}
for( int i = 0; i < len; i++ ) {
write( theBytes[ off + i ] );
}
}
public void flushBase64() throws java.io.IOException {
if( position > 0 ) {
if( encode ) {
out.write( Base64.encode3to4( b4, buffer, position, options ) );
position = 0;
}
else {
throw new java.io.IOException( "Base64 input not properly padded." );
}
}
}
@Override
public void close() throws java.io.IOException {
flushBase64();
super.close();
buffer = null;
out = null;
}
public void suspendEncoding() throws java.io.IOException {
flushBase64();
this.suspendEncoding = true;
}
public void resumeEncoding() {
this.suspendEncoding = false;
}
}
2. Utils.java
package com.abhan.example;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import android.os.Environment;
public class Utils {
public static boolean checkExternalStorageAvailable() {
boolean mExternalStorageAvailable = false;
String state = Environment.getExternalStorageState();
if(state.equals(Environment.MEDIA_MOUNTED)) {
mExternalStorageAvailable = true;
} else if(state.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
mExternalStorageAvailable = false;
} else if(state.equals(Environment.MEDIA_NOFS)) {
mExternalStorageAvailable = false;
} else if(state.equals(Environment.MEDIA_SHARED)) {
mExternalStorageAvailable = false;
} else if(state.equals(Environment.MEDIA_UNMOUNTABLE)) {
mExternalStorageAvailable = false;
} else if(state.equals(Environment.MEDIA_UNMOUNTED)) {
mExternalStorageAvailable = false;
} else if(state.equals(Environment.MEDIA_REMOVED)) {
mExternalStorageAvailable = false;
}
return mExternalStorageAvailable;
}
public static String dataDirectory() {
String dataDir = null;
if(checkExternalStorageAvailable()) {
dataDir = Environment.getDataDirectory().getAbsolutePath();
}
return dataDir;
}
public static boolean createDirectory(final String dirName) {
boolean isDirectoryCreated = false;
if(checkExternalStorageAvailable()) {
File dirFile = new File(dirName);
if(!dirFile.exists()) {
dirFile.mkdirs();
isDirectoryCreated = true;
}
}
return isDirectoryCreated;
}
public static boolean deleteDirectory(final String dirName) {
boolean isDirectoryDeleted = false;
if(checkExternalStorageAvailable()) {
File dirFile = new File(dirName);
if(!dirFile.exists()) {
isDirectoryDeleted = false;
} else {
dirFile.delete();
isDirectoryDeleted = true;
}
}
return isDirectoryDeleted;
}
public static String todayDate(final String providedFormat) {
String returnedDateString = null;
try {
Calendar calendar = new GregorianCalendar();
DateFormat dateFormat = new SimpleDateFormat(providedFormat);
returnedDateString = dateFormat.format(calendar.getTime());
} catch (Exception e) {
e.printStackTrace();
}
return returnedDateString;
}
public static byte[] convertDatabaseFileToArray(final String filePath) {
InputStream inputStream = null;
ByteArrayOutputStream byteOutputStream = null;
try {
inputStream = new BufferedInputStream(
new FileInputStream(new File(filePath)), 8192);
byte[] buffer = new byte[8096];
byteOutputStream = new ByteArrayOutputStream();
int length = 0;
while((length = inputStream.read(buffer)) != -1) {
byteOutputStream.write(buffer, 0, length);
}
if(inputStream != null) {
inputStream.close();
}
if(byteOutputStream != null) {
byteOutputStream.flush();
byteOutputStream.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return byteOutputStream.toByteArray();
}
}
3. Base64.java
要下载此文件,您可以使用GrepCode Search。
根据您在 Web 服务中的需要使用编码字节/字符串Base64
并将其传递给您的参数。
在MainActivity.java
更改以下内容。
final String filePath = Utils.dataDirectory() + File.separator + "data"
+ File.separator + MainActivity.this.getPackageName()
+ File.separator + "databases" + File.separator + "My_DB";
final String todayDate = Utils.todayDate("Your_Preferred_Format");
final String param0 = "http://121.121.121.121/mWebService/Abhan.asmx";
final String param1 = "121.121.121.121";
final String param2 = "http://localhost/mWebService/UploadFile";
final String param3 = getTopOfHeader()
+ "<UploadFile xmlns=\"http://localhost/mWebService\">"
+ "<FileArray>" + Base64.encodeBytes(Utils.convertDatabaseFileToArray(filePath)) + "</FileArray>"
+ "<Filename>" + todayDate + "_1234567890_My_DB.db" + "</Filename>"
+ "<Number>1234567890</Number>"
+ "</UploadFile>"
+ getBottomOfHeader();
在这个演示中,我使用了Local Service
. 请将其更改为您的Live
或Local
Web 服务。此外,我还在Database
. \data\data\databases\My_DB
您可以删除不必要的代码。
运行演示。
谢谢。
于 2013-04-18T06:16:32.057 回答