out = new FileOutputStream(f, true);
content = req.getInputStream();
int read = 0;
final byte[] bytes = new byte[CommonUtils.BUFFER_LENGTH];
while ((read = content.read(bytes)) != -1)
out.write(bytes, 0, read);
start = f.length();
客户端代码
HttpURLConnection connection = getConnect(setUrl(fileUploadMap, uploadUrl));
connection.setRequestMethod("POST");
connection.setDoOutput(true);
// pos上回读取传送的位置 blob每次读取多少字节
String range = "bytes " + start + "-" + (start + 153600) + "/" + file.length();
connection.setRequestProperty("content-range", range);
// 获取输出流对象,预备上传文件
OutputStream os = connection.getOutputStream();
FileInputStream fis = new FileInputStream(file);
int count = 0;
if(file.length() > 10240){
while ((count = fis.read(bytes)) != -1)
os.write(bytes, 0, count);
}else{
while((count = fis.read()) != -1)
os.write(bytes, 0, count);
}
fis.close();
os.flush();
思路分割文件之后按固定字节上传到服务器,服务器会生成一个临时文件,每次上传会先获取token就是临时文件名,包含文件总的大小,可以获取到已保存的大小,给到客户端客户端决定从什么位置开始再次上传,目前分割文件还没做,服务器端是经过网页请求测试大小文件,重新上传都ok的,客户端代码写到了获取token获取再次上传文件的位置以及上传,当测试上传文件的时候发现上传2m一下的文件,客户端debug,outputstream.write的时候有内容,在服务器request.getinputStream获取不到,(read = content.read(bytes)) != -1这里判断直接false了,请问大家这是怎么回事啊??!!