|
| 1 | +package io.udfs.api.util; |
| 2 | + |
| 3 | +import org.apache.http.HttpEntity; |
| 4 | +import org.apache.http.HttpResponse; |
| 5 | +import org.apache.http.client.methods.HttpUriRequest; |
| 6 | +import org.apache.http.client.methods.RequestBuilder; |
| 7 | +import org.apache.http.entity.ContentType; |
| 8 | +import org.apache.http.entity.mime.HttpMultipartMode; |
| 9 | +import org.apache.http.entity.mime.MultipartEntity; |
| 10 | +import org.apache.http.entity.mime.MultipartEntityBuilder; |
| 11 | +import org.apache.http.entity.mime.content.FileBody; |
| 12 | +import org.apache.http.impl.client.CloseableHttpClient; |
| 13 | +import org.apache.http.impl.client.HttpClients; |
| 14 | +import org.apache.http.impl.client.LaxRedirectStrategy; |
| 15 | +import org.apache.http.util.EntityUtils; |
| 16 | + |
| 17 | +import java.io.File; |
| 18 | +import java.io.IOException; |
| 19 | +import java.io.OutputStream; |
| 20 | +import java.net.HttpURLConnection; |
| 21 | +import java.net.URL; |
| 22 | + |
| 23 | +public class MultipartUploadExample { |
| 24 | + public static void main(String[] args) throws Exception { |
| 25 | + //Creating CloseableHttpClient object |
| 26 | + //CloseableHttpClient httpclient = HttpClients.createDefault(); |
| 27 | + CloseableHttpClient httpclient = HttpClients.custom() |
| 28 | + .addInterceptorFirst(new ContentLengthHeaderRemover()).setRedirectStrategy(new LaxRedirectStrategy()) |
| 29 | + .build(); |
| 30 | + |
| 31 | + //Creating a file object |
| 32 | + //String url="http://114.67.38.19:15001/api/v0/add"; |
| 33 | + //String url="http://124.232.153.109:15001/api/v0/add"; |
| 34 | + String url="http://api.udfs.one:15001/api/v0/delete/"; |
| 35 | + //String filePath = "F:\\分布式存储\\宣传片\\少林寺BD国粤双语中字[电影天堂www.dy2018.com].mkv"; |
| 36 | + String hash="QmWYJiC3itS1CNr5ys7c4eAr98f2D8q1PndgnRoGtJZpwC"; |
| 37 | + |
| 38 | + /*String filePath1 = "F:\\分布式存储\\宣传片\\temp\\Ulord链云生态宣传片中英文字幕200M-0.mp4"; |
| 39 | + String filePath2 = "F:\\分布式存储\\宣传片\\temp\\Ulord链云生态宣传片中英文字幕200M-1.mp4"; |
| 40 | + String filePath3 = "F:\\分布式存储\\宣传片\\temp\\Ulord链云生态宣传片中英文字幕200M-2.mp4"; |
| 41 | + String filePath4 = "F:\\分布式存储\\宣传片\\temp\\Ulord链云生态宣传片中英文字幕200M-3.mp4"; |
| 42 | + String filePath5 = "F:\\分布式存储\\宣传片\\temp\\Ulord链云生态宣传片中英文字幕200M-4.mp4"; |
| 43 | + String filePath6 = "F:\\分布式存储\\宣传片\\temp\\Ulord链云生态宣传片中英文字幕200M-5.mp4"; |
| 44 | + String filePath7 = "F:\\分布式存储\\宣传片\\temp\\Ulord链云生态宣传片中英文字幕200M-6.mp4"; |
| 45 | + String filePath8 = "F:\\分布式存储\\宣传片\\temp\\Ulord链云生态宣传片中英文字幕200M-7.mp4"; |
| 46 | + String filePath9 = "F:\\分布式存储\\宣传片\\temp\\Ulord链云生态宣传片中英文字幕200M-8.mp4"; |
| 47 | + String filePath10 = "F:\\分布式存储\\宣传片\\temp\\Ulord链云生态宣传片中英文字幕200M-9.mp4"; |
| 48 | + String filePath11 = "F:\\分布式存储\\宣传片\\temp\\Ulord链云生态宣传片中英文字幕200M-10.mp4"; |
| 49 | + String filePath12 = "F:\\分布式存储\\宣传片\\temp\\Ulord链云生态宣传片中英文字幕200M-11.mp4"; |
| 50 | + String filePath13 = "F:\\分布式存储\\宣传片\\temp\\Ulord链云生态宣传片中英文字幕200M-12.mp4"; |
| 51 | + String filePath14 = "F:\\分布式存储\\宣传片\\temp\\Ulord链云生态宣传片中英文字幕200M-13.mp4"; |
| 52 | + String filePath15 = "F:\\分布式存储\\宣传片\\temp\\Ulord链云生态宣传片中英文字幕200M-14.mp4"; |
| 53 | + String filePath16 = "F:\\分布式存储\\宣传片\\temp\\Ulord链云生态宣传片中英文字幕200M-15.mp4"; |
| 54 | + String filePath17 = "F:\\分布式存储\\宣传片\\temp\\Ulord链云生态宣传片中英文字幕200M-16.mp4"; |
| 55 | + String filePath18 = "F:\\分布式存储\\宣传片\\temp\\Ulord链云生态宣传片中英文字幕200M-17.mp4"; |
| 56 | + String filePath19 = "F:\\分布式存储\\宣传片\\temp\\Ulord链云生态宣传片中英文字幕200M-18.mp4";*/ |
| 57 | + //File file = new File(filePath); |
| 58 | + //UdfsDevTools tools = new UdfsDevTools(1000000, "76E18tAYEU2WPLww2DwPvM6", "udisk",filePath, ""); |
| 59 | + //UdfsDevTools tools = new UdfsDevTools(1000000, "76E18tAYEU2WPLww2DwPvM6", "udisk",filePath, ""); |
| 60 | + |
| 61 | + UdfsDevTools tools = new UdfsDevTools(1000, "D3BBEBC1E5004F429D301041B3771D9E", "bcdcentercom", ""); |
| 62 | + |
| 63 | + //Creating the FileBody object |
| 64 | + //FileBody filebody = new FileBody(file, ContentType.DEFAULT_BINARY); |
| 65 | + |
| 66 | + //Creating the MultipartEntityBuilder |
| 67 | + MultipartEntityBuilder entitybuilder = MultipartEntityBuilder.create(); |
| 68 | + |
| 69 | + //Setting the mode |
| 70 | + entitybuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); |
| 71 | + |
| 72 | + //Adding text |
| 73 | + //entitybuilder.addTextBody("sample_text", "This is the text part of our file"); |
| 74 | + |
| 75 | + //Adding a file |
| 76 | + //entitybuilder.addBinaryBody("少林", new File(filePath)); |
| 77 | + /*entitybuilder.addBinaryBody("宣传片2", new File(filePath2)); |
| 78 | + entitybuilder.addBinaryBody("宣传片3", new File(filePath3)); |
| 79 | + entitybuilder.addBinaryBody("宣传片4", new File(filePath4)); |
| 80 | + entitybuilder.addBinaryBody("宣传片5", new File(filePath5)); |
| 81 | + entitybuilder.addBinaryBody("宣传片6", new File(filePath6)); |
| 82 | + entitybuilder.addBinaryBody("宣传片7", new File(filePath7)); |
| 83 | + entitybuilder.addBinaryBody("宣传片8", new File(filePath8)); |
| 84 | + entitybuilder.addBinaryBody("宣传片9", new File(filePath9)); |
| 85 | + entitybuilder.addBinaryBody("宣传片10", new File(filePath10)); |
| 86 | + entitybuilder.addBinaryBody("宣传片11", new File(filePath11)); |
| 87 | + entitybuilder.addBinaryBody("宣传片12", new File(filePath12)); |
| 88 | + entitybuilder.addBinaryBody("宣传片13", new File(filePath13)); |
| 89 | + entitybuilder.addBinaryBody("宣传片14", new File(filePath14)); |
| 90 | + entitybuilder.addBinaryBody("宣传片15", new File(filePath15)); |
| 91 | + entitybuilder.addBinaryBody("宣传片16", new File(filePath16)); |
| 92 | + entitybuilder.addBinaryBody("宣传片17", new File(filePath17)); |
| 93 | + entitybuilder.addBinaryBody("宣传片18", new File(filePath18));*/ |
| 94 | + |
| 95 | + //Building a single entity using the parts |
| 96 | + HttpEntity mutiPartHttpEntity = entitybuilder.build(); |
| 97 | + |
| 98 | + //System.out.println(url+"?token=" + tools.getToken("add").toString()); |
| 99 | + |
| 100 | + //Building the RequestBuilder request object |
| 101 | + RequestBuilder reqbuilder = RequestBuilder.post(url+hash+"?token=" + tools.getToken("get").toString()); |
| 102 | + |
| 103 | + System.out.println(url+hash+"?token=" + tools.getToken("get").toString()); |
| 104 | + |
| 105 | + |
| 106 | + //Set the entity object to the RequestBuilder |
| 107 | + reqbuilder.setEntity(mutiPartHttpEntity); |
| 108 | + |
| 109 | + //Building the request |
| 110 | + HttpUriRequest multipartRequest = reqbuilder.build(); |
| 111 | + |
| 112 | + //Executing the request |
| 113 | + HttpResponse httpresponse = httpclient.execute(multipartRequest); |
| 114 | + |
| 115 | + //Printing the status and the contents of the response |
| 116 | + System.out.println(EntityUtils.toString(httpresponse.getEntity())); |
| 117 | + System.out.println(httpresponse.getStatusLine()); |
| 118 | + //MultipartUploadExample.uploadFile(); |
| 119 | + } |
| 120 | + |
| 121 | + public static int uploadFile() throws Exception { |
| 122 | + String url="http://test.api.udfs.one:15001/api/v0/add"; |
| 123 | + String filePath = "F:\\分布式存储\\宣传片\\Ulord链云生态宣传片中英文字幕200M.mp4"; |
| 124 | + UdfsDevTools tools = new UdfsDevTools(1000000, "test", "testaccountb",filePath, ""); |
| 125 | + HttpURLConnection connection = |
| 126 | + (HttpURLConnection) (new URL(url+"?token=" + tools.getToken("add").toString())).openConnection(); |
| 127 | + connection.setDoOutput(true); |
| 128 | + connection.setRequestMethod("POST"); |
| 129 | + |
| 130 | + FileBody fileBody = new FileBody(new File(filePath)); |
| 131 | + MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.STRICT); |
| 132 | + multipartEntity.addPart("宣传片", fileBody); |
| 133 | + |
| 134 | + connection.setRequestProperty("Content-Type", multipartEntity.getContentType().getValue()); |
| 135 | + OutputStream out = connection.getOutputStream(); |
| 136 | + try { |
| 137 | + multipartEntity.writeTo(out); |
| 138 | + } catch (IOException e) { |
| 139 | + e.printStackTrace(); |
| 140 | + } finally { |
| 141 | + out.close(); |
| 142 | + } |
| 143 | + int status = connection.getResponseCode(); |
| 144 | + return status; |
| 145 | + |
| 146 | + } |
| 147 | + |
| 148 | +} |
0 commit comments