json数据为:
[
{
"date":"2017-09-16",
"locationZ":"we",
"locationQ":"qw",
"id":null,
"time":"12:31",
"userName":"Yu"
},
{
"date":"2017-11-23",
"locationZ":"xf",
"locationQ":"ty",
"id":null,
"time":"12:31",
"userName":"Yu"
},
{
"date":"2017-05-21",
"locationZ":"xf",
"locationQ":"hd",
"id":null,
"time":"12:31",
"userName":"Yu"
}
]
服务器
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/html;charset=utf-8");
PrintWriter writer;
FileInputStream fs;
File file = new File("E://project//12.json");
BufferedReader br;
String path = "E://project//12.json";
fs = new FileInputStream(file);
byte[] image = new byte[1024];
fs.read(image,0,1024);
fs.close();
response.setHeader("Content-Type", "application/octet-stream");
response.setHeader("Content-Disposition","attachment;filename=12.json");
response.setContentType("text/html;charset=utf-8");
response.getOutputStream().write(image);
response.getOutputStream().flush();
response.getOutputStream().close();
Android
下载文件-读取文件-输出文件
public void doDownload() {
Request.Builder builder = new Request.Builder();
final Request request = builder
.get()
.url(OkhttpUse.url + "CarBD1")
.build();
Call call = OkhttpUse.client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e("onFailure: ", e.getMessage());
e.printStackTrace();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.e("onResponse: ", "ennnnnn");
InputStream is = response.body().byteStream();
int len = 0;
File file = new File(Environment.getExternalStorageDirectory(), "/12.json");
byte[] buf = new byte[128];
FileOutputStream fos = new FileOutputStream(file);
while ((len = is.read(buf)) != -1) {
fos.write(buf, 0, len);
}
fos.flush();
fos.close();
is.close();
ReadFile();
}
});
}
public void ReadFile() {
File file = new File(Environment.getExternalStorageDirectory(), "12.json");
BufferedReader br;
String path = Environment.getExternalStorageDirectory() + "/12.json";
try {
FileInputStream fileInputStream = new FileInputStream(Environment.getExternalStorageDirectory() + "/12.json");
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "utf-8");
// FileReader fileReader = new FileReader(file);
br = new BufferedReader(inputStreamReader);
String line;
StringBuilder builder = new StringBuilder();
while ((line = br.readLine()) != null) {
builder.append(line);
}
Log.e("String : ", builder.toString());
br.close();
// inputStreamReader.close();
json = builder.toString();
Json(json);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void Json(String json) {
try {
JSONArray jsonArray = new JSONArray(json);
if(json!=null &&json.startsWith("\ufeff")){
json = json.substring(1);
}
for (int i=0;i<jsonArray.length();i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
date = jsonObject.getString("date");
locationZ = jsonObject.getString("locationZ");
locationQ = jsonObject.getString("locationQ");
id = jsonObject.getString("id");
time = jsonObject.getString("time");
username = jsonObject.getString("userName");
carPerson = new CarPerson();
carPerson.setDate(date);
carPerson.setTime(time);
carPerson.setLocationQ(locationQ);
carPerson.setLocationZ(locationZ);
carPerson.setUserName(username);
carPersons.add(carPerson);
}
} catch (Exception e) {
e.printStackTrace();
}
}