2012-11-20 5 views
1

나는 다음과 같은 코드 작업이 있습니다파일을 Android로 업로드 할 때 파일의 이름을 바꾸려면 어떻게합니까?

 MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 
     entity.addPart("userfile", new FileBody(f)); 
     httppost.setEntity(entity); 
     HttpResponse response = httpclient.execute(httppost); 

문제는 File f의 이름이 "ABC-temp.jpg가"라고하고 나는 그것을 업로드 할 때 "xyz.jpg"를되고 싶어합니다. 그래도 장치의 파일 이름을 바꾸고 싶지는 않지만 만 업로드 할 수 있습니다.

이렇게하는 가장 좋은 방법은 무엇입니까?

+0

당신은 웹 serivecs –

+0

http://stackoverflow.com/questions/2896733/how-to-rename-a-file-on-sdcard-with 사용에 대한 것은 서버에서 이름을 바꿀 어떤 서버에 업로드하는 경우 -android-application –

+0

@mohammed momn -이 시점에서 내가 할 수 없다고 가정합시다. –

답변

0

잘 작동하는지 모르겠지만 FileBody에서 상속 받고 getFilename() 메서드를 재정의하는 클래스를 만들 수 있습니다.

트릭을해야한다고 생각합니다.

1

확실하게 가능합니다. 실제로 해 보았습니다.

    FileBody mFileBody = new FileBody(f); 
        String mUploadFileName = "xyz.jpg"; 

       FormBodyPart mFormBodyPart = new FormBodyPart("userfile", mFileBody) 
       { 
        @Override 
        protected void generateContentDisp(ContentBody body) { 
         StringBuilder buffer = new StringBuilder(); 
         buffer.append("form-data; name=\""); 
         buffer.append("userfile"); 
         buffer.append("\""); 
         buffer.append("; filename=\""+mUploadFileName+"\""); 
         addField(MIME.CONTENT_DISPOSITION, buffer.toString()); 
        } 
       }; 
       multipartContent.addPart(mFormBodyPart); 
관련 문제