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. 骨牌覆盖问题 KxM

    前面我们说了一些简单的骨牌覆盖问题,有了上面的经验,我们可以尝试解决K*M的 思路和上一篇文章所提到的3*N的 很类似: 依然是矩阵快速幂.我们需要把一个小的边固定下来作为的已知边,然后进行矩阵快速幂 ...

  2. vim 使用、设置笔记

    一.设置.vimrc( windows下通常为_vimrc) 1.设置vim中tab的缩进 set ts=4  (注:ts是tabstop的缩写,设TAB宽4个空格) set expandtab (注 ...

  3. eslintrc.js

    此插件主要就是规范前端程序员编写JS的规范,让代码看上去很优雅,也便于后期人员的重构和维护. 因为是用vue的cli搭建项目工程,使用了eslintrc.js ,但是在写JS时发现,首字母缩进一直报错 ...

  4. rsync应用实例

      一. 通过ssh的方式 前面介绍的rsync 5种方式当中,第二.第三(1个冒号)就属于通过ssh的方式,这种方式其实就是让用户去登录到远程机器,然后执行rsync的任务. [root@local ...

  5. hihoCoder2月29日(字符串模拟)

    时间限制:2000ms 单点时限:1000ms 内存限制:256MB 描述 给定两个日期,计算这两个日期之间有多少个2月29日(包括起始日期). 只有闰年有2月29日,满足以下一个条件的年份为闰年: ...

  6. heartbeat3.x部署安装

    使用Heartbeat构建Linux双机热备系统 本文档版本号: V1.0 版 本 历 史 版本号 更新时间 说 明 创建者 V1.0 2013-3-23 修改版 金桥 1 部署环境 OS: Red ...

  7. bzoj2159

    树形dp+第二类斯特林数 又是这种形式,只不过这次不用伯努利数了 直接搞肯定不行,我们化简一下式子,考虑x^n的组合意义,是把n个物品放到x个箱子里的方案数.那么就等于这个i=1->n,sigm ...

  8. httpclient:实现有验证码的模拟登陆

    //1.这种方式是先把验证码的图片下载到本地.并且根据网页解析获得token值//2.手动在控制台输入验证码//3.因为验证码图片已经下载下来,后面就可以使用图像文字识别package DoubanS ...

  9. Spring Boot 学习系列(11)—tomcat参数配置建

    此文已由作者易国强授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 在SpringBoot项目中,使用的是内嵌的Tomcat容器,相关的配置项如下表: 除去和默认值相同的配置, ...

  10. Linux下新建一个站点

    Apache+nagix使用Lnmpa创建一个新的站点 我们在部署服务器的时候通常会遇到需要分域名和分应用部署,那么如何通过Apache+nagix创建一个新的站点服务呢 LNMPA这种架构有什么优势 ...