공개 클래스는 {
public static String TAG = "filevideoupload";
private static final char PARAMETER_DELIMITER = '&';
private static final char PARAMETER_EQUALS_CHAR = '=';
public String sendFileToServer(String filename, String targetUrl, String videoName)
{
String response = "error";
String postParameters;
Log.e(TAG, filename);
Log.e(TAG, targetUrl);
long start = System.currentTimeMillis();
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
// DataInputStream inputStream = null;
String pathToOurFile = filename;
String urlServer = targetUrl;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
// DateFormat df = new SimpleDateFormat("yyyy_MM_dd_HH:mm:ss");
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024;
try
{
FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile));
URL url = new URL(urlServer);
connection = (HttpURLConnection) url.openConnection();
// Allow Inputs & Outputs
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setChunkedStreamingMode(1024);
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
outputStream = new DataOutputStream(connection.getOutputStream());
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
String connstr = null;
connstr = "Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + videoName + "\"" + lineEnd;
Log.i(TAG, "connstr->" + connstr);
outputStream.writeBytes(connstr);
outputStream.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// Read file
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
Log.e(TAG, "bytesAvailable " + bytesAvailable + "");
try
{
while (bytesRead > 0)
{
try
{
outputStream.write(buffer, 0, bufferSize);
}
catch (OutOfMemoryError e)
{
e.printStackTrace();
response = "outofmemoryerror";
Log.e(TAG, "115 OOM ");
return response;
}
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
}
catch (Exception e)
{
Log.e(TAG, "bytesAvailable error in try 124" + bytesAvailable + "");
e.printStackTrace();
response = "error";
return response;
}
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
Log.e(TAG, "112 --> end of wrte");
// Responses from the server (code and message)
int serverResponseCode = connection.getResponseCode();
Log.i(TAG, "server response code " + serverResponseCode);
Log.i(TAG, "server response msg " + connection.getResponseMessage());
if (serverResponseCode == 200)
{
response = "true";
}
InputStream is;
if (connection.getResponseCode() != 200)
{
is = connection.getErrorStream();
}
else
{
is = connection.getInputStream();
}
// s.useDelimiter("\\Z");
Log.i(TAG, "server response content " + getStringFromInputStream(is));
fileInputStream.close();
outputStream.flush();
outputStream.close();
outputStream = null;
}
catch (Exception ex)
{
// Exception handling
response = "error";
Log.e(TAG, ex.getMessage() + "");
ex.printStackTrace();
}
Log.e(TAG, "Time for uplaod " + (System.currentTimeMillis() - start)/1000 + " sekund:)");
return response;
}
private String getStringFromInputStream(InputStream is)
{
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
String line;
try
{
br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null)
{
sb.append(line);
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (br != null)
{
try
{
br.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
return sb.toString();
}
}
을 사용하여 파일 업로드를위한이 클래스를 BigFileUpload. 비디오 업로드에 사용합니다.
작동하지 않습니다.
@Marcin Orlowski 질문에 문제가 있습니까? –
그것은이었다. 코드 블록 서식 지정. 당신은 항상 "편집 ...."을 클릭하여 편집의 차이를 볼 수 있습니다 –
하지만 이전도 읽을 수있는 형식입니까? –