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. SSL handshake_decode_error

    查看布署在Amazon服务器上的日志时,发现如下错误: generated SERVER ALERT: Fatal - Handshake Failure - handshake_decode_err ...

  2. UOJ309 UNR #2 排兵布阵

    包含不小于$\sqrt n$列的只有不大于$\sqrt n$行,修改时这些行打标记,否则暴力更新,操作一列的时候暴力更新这些行.合并没啥影响直接搞就是了.更新需要访问位置,感觉必须用哈希表,并不是特别 ...

  3. Linux中进程控制块PCB-------task_struct结构体结构

    Linux中task_struct用来控制管理进程,结构如下: struct task_struct { //说明了该进程是否可以执行,还是可中断等信息 volatile long state; // ...

  4. python虚拟环境管理包virtualenvwrapper

    1.打开cmd 2.安装virtualenvwrapper pip install virtualenvwrapper-win 3.配置虚拟环境的位置 新建系统变量默认在c盘 4.新建虚拟环境 mkv ...

  5. JSON标准格式

    标准JSON的合法符号:{(左大括号)  }(右大括号)  "(双引号)  :(冒号)  ,(逗号)  [(左中括号)  ](右中括号) JSON字符串:特殊字符可在字符前面加 \ 或使用 ...

  6. Object.prototype.toString.call(obj)检测数据类型

    typeof bar=='object' 不能确切判断数据是一个‘纯粹’的对象 Array null的结果都是object 比较好的方法是: Object.prototype.toString.cal ...

  7. C++ TUTORIAL - MEMORY ALLOCATION - 2016

    http://www.bogotobogo.com/cplusplus/memoryallocation.php Variables and Memory Variables represent st ...

  8. c/c++面试12-18------关与sizeof那些事儿

    12 使用sizeof计算普通变量所占空间大小 (1)不同数据类型所占字节数不同(32位 64位系统不同) int----->4 double----->8 char-------> ...

  9. IE8 以上版本兼容

    在html的内如下写法 其中最后一行是永远以最新的IE版本模式来显示网页的. 另外加上Emulate模式 Emulate模式后则更重视 (细心的人会注意到,用IE9去访问带有x-ua-compatib ...

  10. AttributeCollection.Add(String, String) Method

    <%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Trans ...