需求描述html
截圖步驟說明java
指定大文件上傳apache
片斷代碼api
運行成功結果服務器
上傳片斷代碼app
1 import java.io.*; 2 import org.apache.http.HttpResponse; 3 import org.apache.http.client.HttpClient; 4 import org.apache.http.client.methods.HttpPost; 5 import org.apache.http.entity.mime.HttpMultipartMode; 6 import org.apache.http.entity.mime.MultipartEntityBuilder; 7 import org.apache.http.impl.client.HttpClients; 8 9 10 public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException { 11 12 Object[] r = getRow(); 13 14 if (r == null) { 15 setOutputDone(); 16 return false; 17 } 18 19 20 r = createOutputRow(r, data.outputRowMeta.size()); 21 22 String urlString = get(Fields.In, "url").getString(r); 23 String filename = get(Fields.In, "filename").getString(r); 24 String filepath = get(Fields.In, "filepath").getString(r); 25 // String action = get(Fields.In, "action").getString(r); 26 String userId = get(Fields.In, "userid").getString(r); 27 28 int partsize = 1024 * 1024 * 5; 29 File file = new File(filename); 30 HttpClient httpClient = HttpClients.createDefault(); 31 HttpPost post = new HttpPost(urlString); 32 MultipartEntityBuilder entity = MultipartEntityBuilder.create(); 33 entity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); 34 entity.addTextBody("filepath", filepath); 35 entity.addTextBody("userId", userId); 36 37 38 try { 39 //Make HTTP Call 40 if (file == null || file.getAbsolutePath() == null) { 41 get(Fields.Out, "http_status").setValue(r,"失敗"); 42 } else { 43 44 long filelenght = file.length(); 45 if (filelenght <= partsize) { 46 entity.addBinaryBody("file", file); 47 entity.addTextBody("action", "create"); 48 post = new HttpPost(urlString); 49 post.setEntity(entity.build()); 50 HttpResponse response = httpClient.execute(post); 51 //設置返回值 52 String httpStatusCode = String.valueOf(response.getStatusLine().getStatusCode()); 53 // System.out.println("****上傳完成*************:" + httpStatusCode + "------result:" + result); 54 if(httpStatusCode.equals("200")||httpStatusCode.equals("201")) 55 { 56 get(Fields.Out, "http_status").setValue(r,"成功"); 57 } 58 else 59 { 60 get(Fields.Out, "http_status").setValue(r,"失敗"); 61 } 62 }else { 63 int endPosition = 0;//子文件結束位置 64 int count = (filelenght % partsize != 0) ? (int) (filelenght / partsize + 1) : (int) (filelenght / partsize); 65 66 try { 67 FileInputStream fileInputStream = new FileInputStream(file); 68 int byteslength = 0; 69 byte[] tempbytes = new byte[partsize]; 70 byte[] array = null; 71 int i = 1; 72 73 while ((byteslength = fileInputStream.read(tempbytes)) != -1) { 74 endPosition += partsize; 75 endPosition = (endPosition > filelenght) ? (int) filelenght : endPosition; 76 array = new byte[byteslength]; 77 //System.arraycopy(tempbytes, 0, array, 0, byteslength); 78 entity = MultipartEntityBuilder.create(); 79 if (endPosition == partsize) { 80 entity.addTextBody("action", "create"); 81 } else { 82 entity.addTextBody("action", "append"); 83 } 84 entity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); 85 entity.addTextBody("filepath", filepath); 86 entity.addTextBody("userId", userId); 87 File tempfile = new File(String.valueOf(0)); 88 FileOutputStream temfileStream = new FileOutputStream(tempfile); 89 temfileStream.write(array); 90 entity.addBinaryBody("file", tempfile); 91 post = new HttpPost(urlString); 92 post.setEntity(entity.build()); 93 temfileStream.close(); 94 httpClient = HttpClients.createDefault(); 95 HttpResponse response = httpClient.execute(post); 96 //設置返回值 97 String httpStatusCode = String.valueOf(response.getStatusLine().getStatusCode()); 98 get(Fields.Out, "http_statuscode").setValue(r, httpStatusCode); 99 if (httpStatusCode.equals("200") || httpStatusCode.equals("201")) { 100 get(Fields.Out, "http_status").setValue(r,"成功"); 101 } else { 102 get(Fields.Out, "http_status").setValue(r,"失敗"); 103 break; 104 } 105 i++; 106 } 107 108 }catch (Exception e) { 109 get(Fields.Out, "http_statuscode").setValue(r, -1); 110 get(Fields.Out, "http_status").setValue(r, "失敗:"+e.getMessage()); 111 } 112 113 114 } 115 } 116 117 } catch (Exception e) { 118 //System.out.println("==================" + e.getMessage()); 119 // Set value of HTTP Status to -1 since HTTP Post caused exception 120 get(Fields.Out, "http_statuscode").setValue(r, -1); 121 get(Fields.Out, "http_status").setValue(r, "失敗:"+e.getMessage()); 122 } finally { 123 124 } 125 126 127 128 129 130 131 132 133 // get(Fields.Out, "http_statuscode").setValue(r, -1); 134 // get(Fields.Out, "http_status").setValue(r, "失敗"); 135 136 137 138 // Send the row on to the next step. 139 putRow(data.outputRowMeta, r); 140 return true; 141 }
需求描述post
下載片斷代碼ui
1 import org.apache.http.*; 2 import org.apache.http.client.ClientProtocolException; 3 import org.apache.http.client.HttpClient; 4 import org.apache.http.client.methods.HttpGet; 5 import org.apache.http.client.utils.URIBuilder; 6 import org.apache.http.impl.client.HttpClients; 7 8 import java.io.File; 9 import java.io.FileOutputStream; 10 import java.io.IOException; 11 import java.io.InputStream; 12 import java.net.URISyntaxException; 13 14 15 16 public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException { 17 18 Object[] r = getRow(); 19 20 if (r == null) { 21 setOutputDone(); 22 return false; 23 } 24 25 r = createOutputRow(r, data.outputRowMeta.size()); 26 27 String urlString = get(Fields.In, "url").getString(r); 28 String filepath = get(Fields.In, "filepath").getString(r); 29 String folder = get(Fields.In, "folder").getString(r); 30 String filename =""; 31 String userId = get(Fields.In, "userid").getString(r); 32 HttpClient httpClient = HttpClients.createDefault(); 33 34 try { 35 URIBuilder builder = new URIBuilder(urlString+"/" + userId); 36 builder.addParameter("filepath", filepath); 37 HttpGet httpGet = new HttpGet(builder.build()); 38 HttpResponse response = httpClient.execute(httpGet); 39 HttpEntity entity = response.getEntity(); 40 InputStream is = entity.getContent(); 41 filename = getFileName(response); 42 //System.out.println("-----filename--------:" + filename); 43 File file = new File(folder + filename); 44 file.getParentFile().mkdirs(); 45 FileOutputStream fileout = new FileOutputStream(file); 46 byte[] buffer = new byte[1024 * 1024]; 47 int ch = 0; 48 while ((ch = is.read(buffer)) != -1) { 49 fileout.write(buffer, 0, ch); 50 } 51 is.close(); 52 fileout.flush(); 53 fileout.close(); 54 get(Fields.Out, "filename").setValue(r,filename); 55 get(Fields.Out, "http_status").setValue(r, "成功"); 56 } catch (URISyntaxException e) { 57 //e.printStackTrace(); 58 get(Fields.Out, "exception").setValue(r, "失敗:"+e.getMessage()); 59 60 } catch (ClientProtocolException e) { 61 //e.printStackTrace(); 62 get(Fields.Out, "exception").setValue(r, "失敗:"+e.getMessage()); 63 64 } catch (IOException e) { 65 //e.printStackTrace(); 66 get(Fields.Out, "exception").setValue(r, "失敗:"+e.getMessage()); 67 68 } 69 70 71 // Send the row on to the next step. 72 putRow(data.outputRowMeta, r); 73 return true; 74 } 75 76 public static String getFileName(HttpResponse response) { 77 Header contentHeader = response.getFirstHeader("Content-Disposition"); 78 String filename = null; 79 if (contentHeader != null) { 80 HeaderElement[] values = contentHeader.getElements(); 81 if (values.length == 1) { 82 NameValuePair param = values[0].getParameterByName("filename"); 83 if (param != null) { 84 try { 85 filename = param.getValue(); 86 } catch (Exception e) { 87 e.printStackTrace(); 88 } 89 } 90 } 91 } 92 return filename; 93 }