更改Android文件上传中的文件名

时间:2016-12-04 17:33:24

标签: php android variables upload

我想更改在Android应用中上传的文件名称,但我不知道如何操作。

我的android代码中的字符串应该是未来的文件名。我测试了一些东西,但还没有成功。

代码在Async任务中实现(上传本身正在运行)。

    private int serverResponseCode;

public String upLoad2Server(String sourceFileUri) {
    String fileName = sourceFileUri;
    HttpURLConnection conn = null;
    DataOutputStream dos = null;
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary = "*****";
    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    int maxBufferSize = 1 * 1024 * 1024;
    File sourceFile = new File(sourceFileUri);
    if (!sourceFile.isFile()) {
        Log.e("Huzza", "Source file does not exist");
        return null;
    }



    try {
        FileInputStream fileInputStream = new FileInputStream(sourceFile);
        URL url = new URL(Config.UPLOAD_URL);
        conn = (HttpURLConnection) url.openConnection();
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setUseCaches(false);
        conn.setRequestMethod("POST");

        String testName = "ThisCouldBeMyNewFileName";



        conn.setRequestProperty("Connection", "Keep-Alive");
        conn.setRequestProperty("ENCTYPE", "multipart/form-data");
        conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);

        conn.setRequestProperty("myFile", fileName);
        conn.setRequestProperty("myTest", testName);


        dos = new DataOutputStream(conn.getOutputStream());


        //I've been trying some stuff here...
        //OutputStream outputStream = conn.getOutputStream();
        //BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(dos, "UTF-8"));
        //String dataString = URLEncoder.encode("testName", "UTF-8") +"="+URLEncoder.encode(testName,"UTF-8");
        //bufferedWriter .write(dataString);
        //bufferedWriter.flush();
        //bufferedWriter.close();
        //Test end, no success with this one

        dos.writeBytes(twoHyphens + boundary + lineEnd);

        dos.writeBytes("Content-Disposition: form-data; name=\"myFile\";filename=\"" + fileName + "\"" + lineEnd);

        //Tried something like this too, but didn't work either.
        //dos.writeBytes("Content-Disposition: form-data; name=\"myTest\";filename=\"" + testName + "\"" + lineEnd);


        dos.writeBytes(lineEnd);

        bytesAvailable = fileInputStream.available();
        Log.i("Huzza", "Initial .available : " + bytesAvailable);

        bufferSize = Math.min(bytesAvailable, maxBufferSize);
        buffer = new byte[bufferSize];

        bytesRead = fileInputStream.read(buffer, 0,bufferSize);

        while (bytesRead > 0) {
            dos.write(buffer, 0, bufferSize);
            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);
        }

        dos.writeBytes(lineEnd);
        dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

        serverResponseCode = conn.getResponseCode();

        fileInputStream.close();

        bufferedWriter .write(dataString);
        bufferedWriter.flush();


        dos.flush();
        dos.close();
        bufferedWriter.close();
    }

    catch (MalformedURLException ex) {
        ex.printStackTrace();
    }
    catch (Exception e) {
        e.printStackTrace();
    }

    if(serverResponseCode == 200) {
        StringBuilder sb = new StringBuilder();
        try{
            BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while((line = rd.readLine()) != null) {
                sb.append(line);
            }
            rd.close();
        }
        catch(IOException ioex) {

        }
        return sb.toString();
    }
    else {
        return "Could not upload";
    }
}

}

PHP部分

if($_SERVER['REQUEST_METHOD']=='POST'){
$file_name = $_FILES['myFile']['name'];
$file_size = $_FILES['myFile']['size'];
$file_type = $_FILES['myFile']['type'];
$temp_name = $_FILES['myFile']['tmp_name'];

//Either this for the POST attempt
$test_name = $_POST['testName'];

//Or this one, then imagine the upper line not being there
$test_name = $_FILES['myTest']['testName'];


$ext = pathinfo($file_name, PATHINFO_EXTENSION);

$location = "files/";

move_uploaded_file($temp_name, $location . $test_name . "." . $ext);
echo "http://contender.hol.es/Contender/files/".$file_name;
}

提前致谢,祝大家有个美好的一天! :)

0 个答案:

没有答案