package cn.itcast.upload;
import java.io.File;
import java.io.OutputStream;
import java.io.PushbackInputStream;
import java.io.RandomAccessFile;
import java.net.Socket;
import cn.itcast.service.FileService;
import cn.itcast.utils.StreamTool;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class UploadActivity extends Activity {
private static final String TAG = "UploadActivity";
private EditText filenameText;
private TextView resultVew;
private ProgressBar progressBar;
private FileService fileService;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
int size = msg.getData().getInt("size");
progressBar.setProgress(size);
float result = (float)progressBar.getProgress()/ (float)progressBar.getMax();
int p = (int)(result*100);
resultVew.setText(p+"%");
if(progressBar.getProgress()==progressBar.getMax()){
Toast.makeText(UploadActivity.this, R.string.success, 1).show();
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
fileService = new FileService(this);
filenameText = (EditText) this.findViewById(R.id.filename);
resultVew = (TextView) this.findViewById(R.id.result);
progressBar = (ProgressBar) this.findViewById(R.id.uploadbar);
Button button = (Button) this.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String filename = filenameText.getText().toString();
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
File uploadFile = new File(Environment.getExternalStorageDirectory(), filename);
uploadfile(uploadFile);
}else{
Toast.makeText(UploadActivity.this, R.string.sdcarderror, 1).show();
}
}
});
}
private void uploadfile(final File file){
new Thread(new Runnable() {
@Override
public void run() {
try {
progressBar.setMax((int)file.length());
String sourceid = fileService.find(file);
Socket socket = new Socket("192.168.1.157", 7878);
OutputStream outStream = socket.getOutputStream();
String head = "Content-Length="+ file.length() + ";filename="+ file.getName() + ";sourceid="+
(sourceid==null? "" : sourceid)+"\r\n";
outStream.write(head.getBytes());
PushbackInputStream inStream = new PushbackInputStream(socket.getInputStream());
String response = StreamTool.readLine(inStream);
System.out.println(response);
String[] items = response.split(";");
String id = items[0].substring(items[0].indexOf("=")+1);//服务返回绑定该文件的资源id
String position = items[1].substring(items[1].indexOf("=")+1);
if(sourceid==null) fileService.save(file, id);
RandomAccessFile fileOutStream = new RandomAccessFile(file, "r");
fileOutStream.seek(Integer.valueOf(position));
byte[] buffer = new byte[1024];
int len = -1;
int length = Integer.valueOf(position);
while( (len = fileOutStream.read(buffer)) != -1){
outStream.write(buffer, 0, len);
length += len;
Message msg = new Message();
msg.getData().putInt("size", length);
handler.sendMessage(msg);
}
fileOutStream.close();
outStream.close();
inStream.close();
socket.close();
if(length==file.length()) fileService.delete(file);
} catch (Exception e) {
Log.e(TAG, e.toString());
}
}
}).start();
}
}
荣华富贵8
- 粉丝: 223
- 资源: 7653
最新资源
- 2025计量基础知识考试题库及答案.doc
- 2025金属冶炼(炼钢)安全员考试题库(含答案).pptx
- 2025健康管理师三级专业能力考核试卷及答案.doc
- 2025交管12123驾驶证学法减分题库附含答案.doc
- 建筑工程员工工资表.xls
- 工程部薪酬2018年6月.doc
- 工程施工操作员薪酬管理制度.doc
- 2025教育心理学与德育工作基础知识点大全.doc
- 2025教育心理学与德育工作基础知识点整理总复习资料.doc
- 2025基本公共卫生知识考试题及答案.docx
- 2025基本公共卫生知识题库及答案.docx
- 2025基础知识与规范要求技能大赛题库及答案.docx
- 2025脊柱术后脑脊液漏应急预案考试试题(含答案).docx
- 2025计量基础知识题库及答案.docx
- 2025计算机二级考试全真试题库及答案(通用版).docx
- 2025计算机基础理论信息安全基本知识试题及答案.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈