1. Rest大文件上传(RestUploadFile.ktr)

需求描述

  1. 上传文件大于10M小于500M
  2. 上传文件进行分片(5M一片要比1M分片整体时间快)
  3. 先使用java类进行功能模拟在迁移Ktr
  4. 使用Kettle+Java片段代码开发
  5. 启动步骤时可以自定义必须参数
  6. 增加UserId(如:testXiaoYu目录)
  7. 上传地址:http://**:8089/api/dlapiservice/v1/file/userdata
  8. 下载地址:http://**:8089/api/dlapiservice/v1/file/userdata/
  9. HDFS地址:http://**:50070/explorer.html#/testXiaoYu(需要查看需要92服务器远程到153服务器查看)

截图步骤说明

  指定大文件上传

  片段代码

  运行成功结果

上传片段代码

 import java.io.*;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.HttpClients; public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException { Object[] r = getRow(); if (r == null) {
setOutputDone();
return false;
} r = createOutputRow(r, data.outputRowMeta.size()); String urlString = get(Fields.In, "url").getString(r);
String filename = get(Fields.In, "filename").getString(r);
String filepath = get(Fields.In, "filepath").getString(r);
// String action = get(Fields.In, "action").getString(r);
String userId = get(Fields.In, "userid").getString(r); int partsize = 1024 * 1024 * 5;
File file = new File(filename);
HttpClient httpClient = HttpClients.createDefault();
HttpPost post = new HttpPost(urlString);
MultipartEntityBuilder entity = MultipartEntityBuilder.create();
entity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addTextBody("filepath", filepath);
entity.addTextBody("userId", userId); try {
//Make HTTP Call
if (file == null || file.getAbsolutePath() == null) {
get(Fields.Out, "http_status").setValue(r,"失败");
} else { long filelenght = file.length();
if (filelenght <= partsize) {
entity.addBinaryBody("file", file);
entity.addTextBody("action", "create");
post = new HttpPost(urlString);
post.setEntity(entity.build());
HttpResponse response = httpClient.execute(post);
//设置返回值
String httpStatusCode = String.valueOf(response.getStatusLine().getStatusCode());
// System.out.println("****上传完成*************:" + httpStatusCode + "------result:" + result);
if(httpStatusCode.equals("200")||httpStatusCode.equals("201"))
{
get(Fields.Out, "http_status").setValue(r,"成功");
}
else
{
get(Fields.Out, "http_status").setValue(r,"失败");
}
}else {
int endPosition = 0;//子文件结束位置
int count = (filelenght % partsize != 0) ? (int) (filelenght / partsize + 1) : (int) (filelenght / partsize); try {
FileInputStream fileInputStream = new FileInputStream(file);
int byteslength = 0;
byte[] tempbytes = new byte[partsize];
byte[] array = null;
int i = 1; while ((byteslength = fileInputStream.read(tempbytes)) != -1) {
endPosition += partsize;
endPosition = (endPosition > filelenght) ? (int) filelenght : endPosition;
array = new byte[byteslength];
//System.arraycopy(tempbytes, 0, array, 0, byteslength);
entity = MultipartEntityBuilder.create();
if (endPosition == partsize) {
entity.addTextBody("action", "create");
} else {
entity.addTextBody("action", "append");
}
entity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addTextBody("filepath", filepath);
entity.addTextBody("userId", userId);
File tempfile = new File(String.valueOf(0));
FileOutputStream temfileStream = new FileOutputStream(tempfile);
temfileStream.write(array);
entity.addBinaryBody("file", tempfile);
post = new HttpPost(urlString);
post.setEntity(entity.build());
temfileStream.close();
httpClient = HttpClients.createDefault();
HttpResponse response = httpClient.execute(post);
//设置返回值
String httpStatusCode = String.valueOf(response.getStatusLine().getStatusCode());
get(Fields.Out, "http_statuscode").setValue(r, httpStatusCode);
if (httpStatusCode.equals("200") || httpStatusCode.equals("201")) {
get(Fields.Out, "http_status").setValue(r,"成功");
} else {
get(Fields.Out, "http_status").setValue(r,"失败");
break;
}
i++;
} }catch (Exception e) {
get(Fields.Out, "http_statuscode").setValue(r, -1);
get(Fields.Out, "http_status").setValue(r, "失败:"+e.getMessage());
} }
} } catch (Exception e) {
//System.out.println("==================" + e.getMessage());
// Set value of HTTP Status to -1 since HTTP Post caused exception
get(Fields.Out, "http_statuscode").setValue(r, -1);
get(Fields.Out, "http_status").setValue(r, "失败:"+e.getMessage());
} finally { } // get(Fields.Out, "http_statuscode").setValue(r, -1);
// get(Fields.Out, "http_status").setValue(r, "失败"); // Send the row on to the next step.
putRow(data.outputRowMeta, r);
return true;
}

2. Rest文件下载(FileDownload.ktr)

需求描述

  1. 下载上传的文件
  2. (5M一片要比1M分片整体时间快)
  3. 下载地址:http://**:8089/api/dlapiservice/v1/file/userdata/
  4. HDFS地址:http:// **:50070/explorer.html#/testXiaoYu(需要查看需要92服务器远程到153服务器查看)

下载片段代码

 import org.apache.http.*;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.HttpClients; import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException; public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException { Object[] r = getRow(); if (r == null) {
setOutputDone();
return false;
} r = createOutputRow(r, data.outputRowMeta.size()); String urlString = get(Fields.In, "url").getString(r);
String filepath = get(Fields.In, "filepath").getString(r);
String folder = get(Fields.In, "folder").getString(r);
String filename ="";
String userId = get(Fields.In, "userid").getString(r);
HttpClient httpClient = HttpClients.createDefault(); try {
URIBuilder builder = new URIBuilder(urlString+"/" + userId);
builder.addParameter("filepath", filepath);
HttpGet httpGet = new HttpGet(builder.build());
HttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
filename = getFileName(response);
//System.out.println("-----filename--------:" + filename);
File file = new File(folder + filename);
file.getParentFile().mkdirs();
FileOutputStream fileout = new FileOutputStream(file);
byte[] buffer = new byte[1024 * 1024];
int ch = 0;
while ((ch = is.read(buffer)) != -1) {
fileout.write(buffer, 0, ch);
}
is.close();
fileout.flush();
fileout.close();
get(Fields.Out, "filename").setValue(r,filename);
get(Fields.Out, "http_status").setValue(r, "成功");
} catch (URISyntaxException e) {
//e.printStackTrace();
get(Fields.Out, "exception").setValue(r, "失败:"+e.getMessage()); } catch (ClientProtocolException e) {
//e.printStackTrace();
get(Fields.Out, "exception").setValue(r, "失败:"+e.getMessage()); } catch (IOException e) {
//e.printStackTrace();
get(Fields.Out, "exception").setValue(r, "失败:"+e.getMessage()); } // Send the row on to the next step.
putRow(data.outputRowMeta, r);
return true;
} public static String getFileName(HttpResponse response) {
Header contentHeader = response.getFirstHeader("Content-Disposition");
String filename = null;
if (contentHeader != null) {
HeaderElement[] values = contentHeader.getElements();
if (values.length == 1) {
NameValuePair param = values[0].getParameterByName("filename");
if (param != null) {
try {
filename = param.getValue();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
return filename;
}

  

  1. KettleDemo整体原型前提条件,Java片段代码需要引用Jar包下载路径: http://hc.apache.org/downloads.cgi

  1. Jar包拷贝

Kettle Rest大文件上传(RestUploadFile.ktr) Rest文件下载(FileDownload.ktr)的更多相关文章

  1. 解决PHP大文件上传问题

    PHP大文件上传问题    今天负责创业计划大赛的老师问我作品上报系统上传不了大文件,我当时纳闷了,做的时候没限制上传文件的大小阿,怎么会传不了呢,自己亲自体验了番,果然不 行,想了好一会儿才有点眉目 ...

  2. 使用commons-fileupload包进行大文件上传注意事项

    项目中使用 commons-fileupload-1.2.1.jar 进行大文件上传. 测试了一把,效果很不错. 总结如下: 必须设置好上传文件的最大阀值 final long MAX_SIZE = ...

  3. 因用了NeatUpload大文件上传控件而导致Nonfile portion > 4194304 bytes错误的解决方法

    今天遇到一个问题,就是“NeatUpload大文件上传控件而导致Nonfile portion > 4194304 bytes错误”,百度后发现了一个解决方法,跟大家分享下: NeatUploa ...

  4. ASP.NET 大文件上传的简单处理

    在 ASP.NET 开发的过程中,文件上传往往使用自带的 FileUpload 控件,可是用过的人都知道,这个控件的局限性十分大,最大的问题就在于上传大文件时让开发者尤为的头疼,而且,上传时无法方便的 ...

  5. 【原创】用JAVA实现大文件上传及显示进度信息

    用JAVA实现大文件上传及显示进度信息 ---解析HTTP MultiPart协议 (本文提供全部源码下载,请访问 https://github.com/grayprince/UploadBigFil ...

  6. BootStrap Progressbar 实现大文件上传的进度条

    1.首先实现大文件上传,如果是几兆或者几十兆的文件就用基本的上传方式就可以了,但是如果是大文件上传的话最好是用分片上传的方式.我这里主要是使用在客户端进行分片读取到服务器段,然后保存,到了服务器段读取 ...

  7. 使用NeatUpload控件实现ASP.NET大文件上传

    使用NeatUpload控件实现ASP.NET大文件上传 一般10M以下的文件上传通过设置Web.Config,再用VS自带的FileUpload控件就可以了,但是如果要上传100M甚至1G的文件就不 ...

  8. Asp.net mvc 大文件上传 断点续传

    Asp.net mvc 大文件上传 断点续传 进度条   概述 项目中需要一个上传200M-500M的文件大小的功能,需要断点续传.上传性能稳定.突破asp.net上传限制.一开始看到51CTO上的这 ...

  9. 百万行mysql数据库优化和10G大文件上传方案

    百万行mysql数据库优化和10G大文件上传方案 最近这几天正在忙这个优化的方案,一直没时间耍,忙碌了一段时间终于还是拿下了这个项目?项目中不要每次都把程序上的问题,让mysql数据库来承担,它只是个 ...

随机推荐

  1. UVA-11892(组合游戏)

    题意: 给n堆石子,每堆有ai个,每次可以取每堆中任意数目的石子;但是上一次操作的人没有将一堆全部取走,那么下一个人还要在那一堆取; 思路: 每次取到这堆就剩一个的策略; AC代码: #include ...

  2. CISCO-更新路由器IOS

    1,查看flash,复制IOS文件名,再上传IOS 2,传送完毕查看下flash Router# show flash: 查看flash中的信息 Directory of flash: 1 -rw- ...

  3. 乐曲主题Musical Themes

    SA例题 题面 对于串 \(S\) 的两个子串 \(A\) 和 \(B\) ,满足 \(k = |A| = |B|\),\(\exists c \forall i\, a_i + c=b_i\),且 ...

  4. 记录ubuntu16.04版本安装过程中遇到的问题

    记录ubuntu16.04版本安装和使用过程中遇到的些问题,方便以后查看,主要内容有: 1. ubuntu源替换 2. windows与vmware ubuntu文件夹共享 3. putty连接ubu ...

  5. vue中的 v-if VS v-show

    相同点:都是动态显示DOM元素. 不同点:1.v-if是动态的向DOM树内添加或者删除DOM元素:v-show是通过设置DOM元素的display样式属性控制显隐: 2.v-if切换有一个局部编译/卸 ...

  6. 苹果app(iOS app)的URL schemes

    最近折腾iOS快捷启动应用或应用内的某个动作的神器launch center pro (LCP),发现很多国产app并没有被LCP官方收录,所以不得不想办法找到app的url schemes. 下面是 ...

  7. c# aop讲解

    先说下场景,C#中为什么要使用Aop,而我又是在哪里使用Aop? 本人只是想拦截实体类的Set的方法,然后在Set之前,调用一下其它方法,把值赋给另一个对象. 而我做的都是在实体类的基类里处理: 比如 ...

  8. HUD-1548

    A strange lift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  9. 【网络爬虫】【java】微博爬虫(二):如何抓取HTML页面及HttpClient使用

    一.写在前面 上篇文章以网易微博爬虫为例,给出了一个很简单的微博爬虫的爬取过程,大概说明了网络爬虫其实也就这么回事,或许初次看到这个例子觉得有些复杂,不过没有关系,上篇文章给的例子只是让大家对爬虫过程 ...

  10. 把myeclipse中html/jsp文件的视图调到只看代码

    烦恼———————————————————— 解决方法: ok---------------------------- *.jsp 同理