关于android上传视频的问题,在线等急。。。

dh1314al 泓进 移动客户端工程师  2011-01-07 02:01:13
情况是这样的,我用android上传一段视频到服务器上,服务器可以成功接收到该视频文件,但是文件大小变小了,也不能正常播放!
public class UploadActivity extends Activity

{

Button btnPost=null;

Button btnPost1=null;

@Override

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

btnPost=(Button) findViewById(R.id.btnPost);

btnPost1=(Button) findViewById(R.id.btnPost1);

btnPost1.setOnClickListener(new OnClickListener()

{

@Override

public void onClick(View v)

{

String actionUrl="http://192.168.1.100:8080/web/upload.do";

File file=new File("/sdcard/11.jpg");

post1(actionUrl,file);

}

});

btnPost.setOnClickListener(new OnClickListener()

{

@Override

public void onClick(View v)

{

String actionUrl="http://192.168.1.100:8080/web/upload.do";

try {

postFile(actionUrl);

} catch (IOException e) {

e.printStackTrace();

}

}

});

}

public void postFile(String actionUrl) throws IOException

{

String BOUNDARY=java.util.UUID.randomUUID().toString();

String MULTIPART_FROM_DATA="multipart/form-data";



URL uri=new URL(actionUrl);



HttpURLConnection conn=(HttpURLConnection) uri.openConnection();



conn.setReadTimeout(5*10000);

conn.setDoInput(true); //允许输入

conn.setDoOutput(true); //允许输出

conn.setUseCaches(false);

conn.setRequestMethod("POST"); //设置请求方式

conn.setRequestProperty("connection","keep-alive");

conn.setRequestProperty("Charsert","UTF-8");



conn.setRequestProperty("Content-Type",MULTIPART_FROM_DATA+";boundary="+BOUNDARY);



DataOutputStream outStream = new DataOutputStream(conn.getOutputStream());

byte[] buffer=new byte[1024];

// InputStream is=new FileInputStream(new File("/sdcard/abc.mp4"));

InputStream is=new FileInputStream(new File("/sdcard/11.jpg"));

int len=0;

while((len=is.read(buffer))!=-1)

{

outStream.write(buffer,0,len);

}

is.close();

outStream.flush();

//得到响应码

int res=conn.getResponseCode();

if(res==200)

{

System.out.println("上传完毕");

}



}

public void post1(String actionUrl,File files)

{

DataOutputStream outStream = null;

HttpURLConnection conn = null;

try

{

URL url = new URL(actionUrl);

conn = (HttpURLConnection) url.openConnection();

conn.setDoInput(true);// 允许输入

conn.setDoOutput(true);// 允许输出

conn.setUseCaches(true);// 不使用Cache

conn.setRequestMethod("POST");

conn.setRequestProperty("Connection", "Keep-Alive");

conn.setRequestProperty("Charset", "UTF-8");

conn.setRequestProperty("Content-Type","text/xml");



outStream = new DataOutputStream(conn.getOutputStream());

FileInputStream inputFile = new FileInputStream(files);

int c=0;

long count1 = 0;

byte[] buf = new byte[1024];

while ((c = inputFile.read(buf)) != -1)

{

outStream.write(buf,0,c);

outStream.flush();

count1+=c;

System.err.println("write size "+(count1)/1024);

}

inputFile.close();

int cah = conn.getResponseCode();

Log.v("cah:", ""+cah);

if (cah != 200){

throw new RuntimeException("请求url失败");

} else {

InputStream is = conn.getInputStream();

outStream.flush();

int ch;

StringBuilder result = new StringBuilder();

while ((ch = is.read()) != -1) {

result.append((char) ch);

}

System.err.println(result.toString());

}

outStream.close();

conn.disconnect();

} catch (Exception e) {

e.printStackTrace();

} finally {

if(null != conn){

conn.disconnect();

conn = null;

}

if(null!=outStream){

try {

outStream.close();

} catch (IOException e) {

//

}

outStream = null;

}

}

}

这是android端的代码,
//获取输入流,是HTTP协议中的实体内容

ServletInputStream sis=request.getInputStream();

//缓冲区

byte buffer[]=new byte[2048];

// File file=new File("d:\\123.mp4");

File file=new File("d:\\11.jpg");

FileOutputStream fos=new FileOutputStream(file);

int len=0;

//把流里的信息循环读入到文件中

while((len=sis.read(buffer, 0, 2048))!=-1)

{

fos.write(buffer, 0, len);

len=sis.readLine(buffer, 0, 2048);

}

fos.close();

sis.close();
这是我servlet服务端代码,各位大哥帮忙解决一下吧!
...全文
330 点赞 收藏 2
写回复
2 条回复
dh1314al 2011年01月08日
谢谢 你的回答!不是这个原因,希望高手给解决一下吧!!
回复 点赞
bean 2011年01月07日
客服端是 byte[] buf = new byte[1024];
服务器端是 byte buffer[]=new byte[2048];
是不是这里的原因呢 改为一样试试
回复 点赞
发动态
发帖子
Android
创建于2009-10-09

4.6w+

社区成员

9.0w+

社区内容

移动平台 Android
社区公告
暂无公告