之前做過一個電視台app,電視台app每次啟動時會通路伺服器,判斷是否需要下載下傳新版本
但是下載下傳時老是下載下傳失敗,apk包總是下載下傳不下來.到底是什麼原因呢?
伺服器下載下傳接口如下:
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIn5GcuIXY0N3Xu92Yp9CXzV2Zh1WavwVbvNmLllXZ0lmL3gjN5gzN3gjMxcHavw1LcpDc0RHaiojIsJye.png)
/***
* 下載下傳apk
* @param path
* @param request
* @return
* @throws ioexception
*/
@requestmapping(value = "/download"/*, headers = {"content-type=application/json"}*/)
public responseentity<byte[]> download( string path,httpservletrequest request) throws ioexception {
accesslog accesslog=loginto(request);
accesslog.setdescription("下載下傳用戶端");
if(!valuewidget.isnullorempty(request.getcontenttype())&& request.getcontenttype().tolowercase().contains("application/json")){
string requeststr=webservletutil.getrequestquerystr(request, null);
system.out.println(requeststr);
map querymap=jsonputil.getmapfromjson(requeststr);
if(!valuewidget.isnullorempty(querymap)){
path=(string) querymap.get("path");
}
}
if(valuewidget.isnullorempty(path)){
system.out.println("download failed");
accesslog.setoperateresult("下載下傳失敗,沒有傳遞path參數");
logsave(accesslog, request);
return null;
string realpath =webservletutil.getuploadpath(request, "upload/download/apk", request
.getsession().getservletcontext(), constant2.src_main_webapp);
if(!realpath.endswith(file.separator)){
realpath=realpath+file.separator;
httpheaders headers = new httpheaders();
headers.setcontenttype(mediatype.application_octet_stream);
string fullpath=realpath+path;
system.out.println("download path:"+fullpath);
headers.set(constant2.content_disposition,webservletutil.getcontentdisposition(true, path));
accesslog.setoperateresult("下載下傳成功,下載下傳檔案:"+fullpath+" ,size:"+fileutils.getfilesize2(fullpath));
logsave(accesslog, request);
return new responseentity<byte[]>(fileutils.getbytes4file(fullpath),
headers, httpstatus.created);
}
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIn5GcuIXY0N3Xu92Yp9CXzV2Zh1WavwVbvNmLllXZ0lmL3gjN5gzN3gjMxcHavw1LcpDc0RHaiojIsJye.png)
*
* @param huc
* @param sendbytes
* @param mode
* @param iswrite2file
* : 是否寫入檔案
* @throws exception
private static byte[] connection(httpurlconnection huc,
boolean iswrite2file, object file, string sizeheadkey)
throws exception {
int rescode = huc.getresponsecode();
if (rescode == httpurlconnection.http_ok) {
int contentlength = 0;
if (valuewidget.isnullorempty(sizeheadkey)) {// 若header中沒有size
contentlength = huc.getcontentlength();
} else {
string sizeheadervalue = huc.getheaderfield(sizeheadkey);
if (!valuewidget.isnullorempty(sizeheadervalue)) {
contentlength = integer.parseint(sizeheadervalue);
}
if (isdetail) {
system.out
.println("[connection]contentlength:" + contentlength);
responseheaderfields = huc.getheaderfields();
string downloadheader = "content-disposition";
if (!valuewidget.isnullorempty(responseheaderfields)) {
list<string> contentdispositions = responseheaderfields
.get(downloadheader);
if (!valuewidget.isnullorempty(contentdispositions)) {
string contentdisposition = contentdispositions.get(0);
system.out.println("contentdisposition:"
+ contentdisposition);
system.out.println("contentdisposition convertiso2utf:"
+ systemhwutil
.convertiso2utf(contentdisposition));
system.out
.println("contentdisposition convertiso2gbk: "
+ systemhwutil
.convertiso2gbk(contentdisposition));
}
for (object obj : responseheaderfields.keyset()) {
list<string> list = responseheaderfields.get(obj);
if (!valuewidget.isnullorempty(list)) {
system.out.println(obj + " : "
+ systemhwutil.formatarr(list, ";"));
if (contentlength > 0) {
if (isdetail)
system.out
.println("[httpsocketutil.connection]httputil,contentlength:"
+ contentlength);
// return readdata(huc);
file file2 = null;
if (iswrite2file) {
if (file instanceof file) {
file2 = (file) file;
writefilefromlength(huc, contentlength, file2);
if (isdetail) {
system.out.println("download success:"
+ file2.getabsolutepath());
}
} else {
writefilefromlength(huc, contentlength,
(outputstream) file);
return null;
} else {
return readdatafromlength(huc, contentlength);
inputstream in = huc.getinputstream();
fileutils.writein2outputcloseall(in, new fileoutputstream(
(file) file));
if (isdetail) {
system.out.println("download success:"
+ ((file) file).getabsolutepath());
return readdata(huc);
} else {
system.out.println("response code:" + rescode);
return null;
代碼本身是沒有邏輯錯誤的.花了很長時間才找到原因,是定義的response的status code不一緻.
修改方法:把背景的status code改為200 就ok了