package com.jqyd.socket;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PushbackInputStream;
import java.io.RandomAccessFile;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import com.jqyd.camera.PhotoUtil;
import com.jqyd.shareInterface.Optdb_interfce;
import com.jqyd.shareInterface.Optsharepre_interface;
import com.jqyd.son.Ddxq;
public class UpSocketFile {
private Context context;
public UpSocketFile(Context context){
this.context = context;
}
public String packageUp(String sort,Bundle bundle){
Log.i("packageUp", "------------packageUp---------------");
String result = "1";
String imagePath = "";
String content = "";
//类别:1、日志上报2、信息反馈3、客户走访(二次回访)4、市场检查5、客户采集
if(sort.equals("rzsb")){
String gguid = bundle.getString("gguid");
String zguid = bundle.getString("zguid");
String cosim = bundle.getString("cosim");
String regsim = bundle.getString("regsim");
String gznr = bundle.getString("gznr");
String ywrq = bundle.getString("ywrq");
String gzsc = bundle.getString("gzsc");
String gzyj = bundle.getString("gzyj");
String gzcg = bundle.getString("gzcg");
String imagecode = bundle.getString("imagecode");
String pic_detail = bundle.getString("pic_detail");
content = ";sort=1;"+"gguid="+gguid+";zguid="+zguid+";cosim="+cosim+";regsim="+regsim+";gznr="+gznr+";ywrq="+ywrq
+";gzsc="+gzsc+";gzyj="+gzyj+";gzcg="+gzcg+";imagecode="+imagecode+";pic_detail="+pic_detail;
imagePath = bundle.getString("imagePath");
File uploadFile = new File(imagePath);
result = uploadFile(uploadFile,content);
}else if(sort.equals("ddxq")){
imagePath = bundle.getString("imagePath");
Optsharepre_interface share_obj = null;
share_obj = new Optsharepre_interface(context);
String cosim = share_obj.getDataFromPres("COSIM");
bundle.putString("cosim", cosim);
File uploadFile = new File(imagePath);
result = uploadFile2(uploadFile,bundle);
}
return result;
}
/**
* 上传文件
* @param uploadFile
*/
private String uploadFile(final File uploadFile,String content) {
//上传结果0、成功1、失败
String result = "1";
try {
Optdb_interfce db = new Optdb_interfce(context);
String souceid = db.getBindId(uploadFile);
String head = "Content-Length="+ uploadFile.length() + ";filename="+ uploadFile.getName() + ";sourceid="+
(souceid==null? "" : souceid)+content+"\r\n";
Log.i("socket", "请求socket连接……");
Socket socket = new Socket();
SocketAddress address = new InetSocketAddress("www.jqgj.com.cn", 9091);
//SocketAddress address = new InetSocketAddress("116.255.134.172", 9091);
socket.connect(address,1000*20);
socket.setSoTimeout(1000*20);
OutputStream outStream = socket.getOutputStream();
outStream.write(head.getBytes());
PushbackInputStream inStream = new PushbackInputStream(socket.getInputStream());
String response = StreamTool.readLine(inStream);
Log.i("Socket", "***********"+response);
String[] items = response.split(";");
String responseid = items[0].substring(items[0].indexOf("=")+1);
String position = items[1].substring(items[1].indexOf("=")+1);
if(souceid==null){//代表原来没有上传过此文件,往数据库添加一条绑定记录
db.insertFileLog(responseid, uploadFile);
}
RandomAccessFile fileOutStream = new RandomAccessFile(uploadFile, "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;
Intent intent2 = new Intent();
intent2.putExtra("length", length);
intent2.setAction("upfile");
context.sendBroadcast(intent2);
}
if(length==uploadFile.length()){
db.deleteFileLog(uploadFile);
}
result = StreamTool.readLine(inStream);
if(result==null){
result="1";
}
Log.e("upSocket", result);
fileOutStream.close();
outStream.close();
inStream.close();
socket.close();
db.close_SqlDb();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
private String uploadFile2(final File uploadFile,Bundle bundle) {
//上传结果0、成功1、失败
String result = "1";
String filename = bundle.getString("filename");
String relationid="1";
if(bundle.getString("relationid")!=null){
relationid=bundle.getString("relationid");
}
String cosim = bundle.getString("cosim")+".jpg";
String type=bundle.getString("type");
Optsharepre_interface share_obj = new Optsharepre_interface(context);
Socket socket = new Socket();
try {
Optdb_interfce db = new Optdb_interfce(context);
String souceid = db.getBindId(uploadFile);
String head = "Content-Length="+ uploadFile.length() + ";filename="+ uploadFile.getName() + ";sourceid="+
(souceid==null? "" : souceid)+";6"+";"+cosim+";"+type+";"+share_obj.getDataFromPres("GUID")+";"+relationid+";"+"\r\n";
Log.i("socket", "请求socket连接……");
// SocketAddress address = new InetSocketAddress("116.255.134.172", 9091);
SocketAddress address = new InetSocketAddress("www.jqgj.com.cn", 9091);
socket.connect(address,1000*20);
socket.setSoTimeout(1000*20);
OutputStream outStream = socket.getOutputStream();
outStream.write(head.getBytes());
PushbackInputStream inStream = new PushbackInputStream(socket.getInputStream());
String response = StreamTool.readLine(inStream);
Log.e("Socket", "***********"+response);
String[] items = response.split(";");
String responseid = items[0].substring(items[0].indexOf("=")+1);
String position = items[1].substring(items[1].indexOf("=")+1);
if(souceid==null){//代表原来没有上传过此文件,往数据库添加一条绑定记录
db.insertFileLog(responseid, uploadFile);
}
RandomAccessFile fileOutStream = new RandomAccessFile(uploadFile, "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;
Log.e("length", length+","+uploadFile.length());
Intent intent2 = new Intent();
intent2.putExtra("length", length);
intent2.setAction("upfile");
context.sendBroadcast(intent2);
}
if(length==uploadFile.length()){
db.deleteFileLog(uploadFile);
}
outStream.flush();
db.close_SqlDb();
result = StreamTool.readLine(inStream);
if(result==null){
result="1";
}
fileOutStream.close();
inStream.close();
outStream.close();
socket.close();
} catch (Exception e) {
e.printStackTrace();
try {
socket.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return "1";
}
return result;
}
}