HttpUrlConnection을 이용하여 수신 파일을 업로드하는 방법

다음과 같습니다.

// 

public static void main(String[] args) throws IOException {
 DataInputStream in = null;
 OutputStream out = null;
 HttpURLConnection conn = null;
 JSONObject resposeTxt = null;
 InputStream ins = null;
 ByteArrayOutputStream outStream = null;
 try {
  URL url = new URL("http://10.28.160.160:9080/main/uploadFile?fileName= .txt");
  conn = (HttpURLConnection) url.openConnection();
  //  POST 
  conn.setDoOutput(true);
  conn.setUseCaches(false);
  conn.setRequestMethod("POST");
  conn.setRequestProperty("Content-Type", "text/html");
  conn.setRequestProperty("Cache-Control", "no-cache");
  conn.setRequestProperty("Charsert", "UTF-8");
  conn.connect();
  conn.setConnectTimeout(10000);
  out = conn.getOutputStream();

  File file = new File("H:/Users/chengtingyu/Desktop/test/list.txt");
  in = new DataInputStream(new FileInputStream(file));

  int bytes = 0;
  byte[] buffer = new byte[1024];
  while ((bytes = in.read(buffer)) != -1) {
  out.write(buffer, 0, bytes);
  }
  out.flush();

  //  
  if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
  ins = conn.getInputStream();
  outStream = new ByteArrayOutputStream();
  byte[] data = new byte[1024];
  int count = -1;
  while ((count = ins.read(data, 0, 1024)) != -1) {
   outStream.write(data, 0, count);
  }
  data = null;
  resposeTxt = JSONObject.parseObject(new String(outStream
   .toByteArray(), "UTF-8"));
  }
 } catch (Exception e) {
  e.printStackTrace();
 } finally {
  if (in != null) {
  in.close();
  }
  if (out != null) {
  out.close();
  }
  if (ins != null) {
  ins.close();
  }
  if (outStream != null) {
  outStream.close();
  }
  if (conn != null) {
  conn.disconnect();
  }
 }
 }

 

// 

 

public String uploadFile() throws Exception{
    String fileName = request.getParameter("fileName");  
    String fileFullPath = "H:/Users/chengtingyu/Desktop/" + fileName;
    InputStream input = null;
    FileOutputStream fos = null;
 try {
  input = request.getInputStream();
  File file = new File("H:/Users/chengtingyu/Desktop");  
     if(!file.exists()){  
       file.mkdirs();  
     }  
     fos = new FileOutputStream(fileFullPath);  
     int size = 0;  
     byte[] buffer = new byte[1024];  
     while ((size = input.read(buffer,0,1024)) != -1) {  
       fos.write(buffer, 0, size);  
     }
     
     //  json 
     Map<String,Object> responseMap = new HashMap<String,Object>();
     responseMap.put("flag", true);
     
     // json 
      String jsonResponse = JSONObject.toJSONString(responseMap);
     sendResponse(jsonResponse);
 } catch (IOException e) {
  //  json 
     Map<String,Object> responseMap = new HashMap<String,Object>();
     responseMap.put("flag", false);
     responseMap.put("errorMsg", e.getMessage());
     String jsonResponse = JSONObject.toJSONString(responseMap);
     sendResponse(jsonResponse);
 } finally{
  if(input != null){
  input.close();
  }
  if(fos != null){
  fos.close();
  }
 }  
      
    return null;
 }
 
 
 
 /**
   *  
   * 
   * @throws Exception
   */
  private void sendResponse(String responseString) throws Exception {
   response.setContentType("application/json;charset=UTF-8");
    PrintWriter pw = null;
    try {
      pw = response.getWriter();
      pw.write(responseString);
      pw.flush();
    } finally {
      IOUtils.closeQuietly(pw);
    }
  }
이상에서 HttpUrlConnection을 이용하여 수신 파일을 업로드하는 실현 방법은 바로 편집자가 여러분에게 공유한 모든 내용입니다. 참고 부탁드리며 많은 응원 부탁드립니다.

좋은 웹페이지 즐겨찾기