caTea 블로그

android file upload and spring(image..) 본문

android

android file upload and spring(image..)

ZaRas 2015. 8. 5. 13:59
반응형

안드로이드에서 쓰는 라이브러리

httpclient-4.5.jar


httpcore-4.4.1.jar


httpmime-4.3.6.jar


웹서버에서 쓰는 라이브러리



cos.jar



int index = filePath.lastIndexOf("/");

String fileName = filePath.substring(index);

File file = new File(filePath);


MultipartEntityBuilder builder = MultipartEntityBuilder.create()

    .setCharset(Charset.forName("UTF-8"))

    .setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

builder.addPart("pictureFile"new FileBody(file));  // 키값은 서버 vo객체 의 변수명과 같아야한다

builder.addTextBody("fileName", fileName);

HttpClient client = AndroidHttpClient.newInstance("Android");

 

HttpPost post = new HttpPost(MAIN_URL+"pictureUpload.do");


post.setEntity(builder.build());

    HttpResponse httpRes = null;

    httpRes = client.execute(post);

    long resultSize = httpRes.getEntity().getContentLength();

    if(resultSize<0){

    StringBuilder sb = new StringBuilder();

    try {

        BufferedReader reader

              new BufferedReader(new InputStreamReader(httpRes.getEntity().getContent()), 65728);

        String line = null;


        while ((line = reader.readLine()) != null) {

            sb.append(line);

        }

    }

    catch (IOException e) { e.printStackTrace(); }

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

    System.out.println("finalResult " + sb.toString());

    return sb.toString();

    }



-------------------------------web server---------------------------

vo


public class PictureFileDTO{

private MultipartFile pictureFile;

private String fileName;

private String filePath;

public MultipartFile getPictureFile(){

return pictureFile;

}

public void setPictureFile(MultipartFile pictureFile){

this.pictureFile = pictureFile;

}

public String getFileName(){

return fileName;

}

public void setFileName(String fileName){

this.fileName = fileName;

}


public String getFilePath() {

return filePath;

}


public void setFilePath(String filePath) {

this.filePath = filePath;

}

}





controller


@RequestMapping(value = "/pictureUpload.do", method = RequestMethod.POST)

@ResponseBody

public String databaseInsert(PictureFileDTO dto){

String returnVal = "{\"ok\" : false, \"msg\": \"Cannot find multipart file\"}";

try{

HttpHeaders responseHeaders = new HttpHeaders();

responseHeaders.add("Content-Type", "application/json; charset=UTF-8");

MultipartFile pictureFile = dto.getPictureFile();

String fileName = dto.getFileName();

if (pictureFile != null) {

String originalFileName = pictureFile.getOriginalFilename();


String contentType = pictureFile.getContentType();

//if (contentType.endsWith("image/jpeg")) { //안드로이드에선 사친첩에서만 보내기때문에 검사 안함

dto.setFileName(originalFileName);

try {

Date now = new Date();

String directory = siteConfig.getImagesPath(); //미리 세팅해둔 이미지 저장 경로

String dateString = new SimpleDateFormat("yyyy-MM-dd").format(now);

String uniqueFileName = UUID.randomUUID().toString();

File dir = new File(directory + "/" + dateString);

if (!dir.exists() || !dir.isDirectory()) {

if (!dir.mkdir())

return returnVal;

}


// 1. FileOutputStream 사용

// byte[] fileData = file.getBytes();

// FileOutputStream output = new FileOutputStream("C:/images/" +fileName);

// output.write(fileData);


// 2. File 사용

File file = new File(directory + "/" + dateString + "/" + uniqueFileName + ".jpg");

pictureFile.transferTo(file);


dto.setFilePath(dateString + "/" + uniqueFileName + ".jpg");

// 데이터 베이스 처리를 현재 위치에서 처리


returnVal = "{\"ok\" : true, \"msg\": \"sucess\"}";

} catch (IOException e) {

e.printStackTrace();

} // try - catch

}

//} // if

}catch(Exception e){

e.printStackTrace();

}

return returnVal;

}

728x90

'android' 카테고리의 다른 글

vertical view pager  (0) 2016.07.01
이미지 라운드 처리 클래스  (0) 2016.06.28
android file path  (0) 2015.08.05
image quality down  (0) 2015.08.05
SwipeMenuListView 서브 메뉴 클릭시 서브메뉴가 닫히지 않는현상  (0) 2015.07.01