java版:
package com.lh.post;
import java.io.*;
import java.net.*;
public class PostFile {
private static byte[] readFile(String file)throws Exception
{
FileInputStream fis = new FileInputStream(file);
byte[] b = new byte[256];
int l;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while((l=fis.read(b)) != -1){
bos.write(b,0,l);
}
fis.close();
return bos.toByteArray();
}
public static void main(String[] args) throws Exception{
URL url = new URL("http://localhost:8080/hellodwr/upload.jsp");
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
//设置请求方式
connection.setRequestMethod("POST");
connection.setDoOutput(true);
//读取文件内容到byte[]
//File file=new File("d:\\s.txt");
//FileOutputStream fos=new FileOutputStream(file);
/*
byte buff;
StringBuffer file_buff =new StringBuffer();
while((buff=fis.read())!=-1)
{
file_buff.append(new String(buff));
}
*/
//byte[] f="abcdefghijklmnopqrstuvwxyz".getBytes();
//byte[] f=readFile("d:\\s.txt");
byte[] f=new FileUtil().getFileByte("d:\\test.doc");
System.out.println("字节数组的大小:"+f.length);
/*
for(int i=0;i<f.length;i++)
{
System.out.print(f[i]);
}
*/
//fos.write(f);
// 分隔符
String BOUNDARY = "---------------------------7d4a6d158c9";
StringBuffer sb = new StringBuffer();
//发送文件:
sb = sb.append("--");
sb = sb.append(BOUNDARY);
sb = sb.append("\r\n");
sb = sb.append("Content-Disposition: form-data; name=\"s\"; filename=\"test.doc\"\r\n");
sb = sb.append("Content-Type: application/octet-stream\r\n\r\n");
byte[] data = sb.toString().getBytes();
byte[] end_data = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();
// 设置HTTP头:
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+ BOUNDARY);
connection.setRequestProperty("Content-Length", String.valueOf(data.length + f.length + end_data.length));
// 输出:
OutputStream out=connection.getOutputStream();
out.write(data);
out.write(f);
out.write(end_data);
out.flush();
// 读取服务器响应:
BufferedReader in=new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line=null;
while((line=in.readLine())!=null){
System.out.println(line);
}
in.close();
out.close();
}
}
class FileUtil {
public byte[] getFileByte(String fileName) throws FileNotFoundException {
FileInputStream fileInputStream = new FileInputStream(fileName);
return getFileByte(fileInputStream);
}
public byte[] getFileByte(URL url) throws IOException {
if (url != null) {
return getFileByte(url.openStream());
} else {
return null;
}
}
public byte[] getFileByte(InputStream in) {
ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
try {
copy(in, out);
} catch (IOException e) {
e.printStackTrace();
}
return out.toByteArray();
}
private void copy(InputStream in, OutputStream out) throws IOException {
try {
byte[] buffer = new byte[4096];
int nrOfBytes = -1;
while ((nrOfBytes = in.read(buffer)) != -1) {
out.write(buffer, 0, nrOfBytes);
}
out.flush();
} catch (IOException e) {
}finally {
try {
if (in != null) {
in.close();
}
} catch (IOException ex) {
}
try {
if (out != null) {
out.close();
}
} catch (IOException ex) {
}
}
}
}
delphi版:
unit UpDownFile;
interface
uses
Windows, Classes, Idhttp, URLMon, IdMultipartFormData;
const UpUrl = 'http://127.0.0.1/upfile/upfile.asp';
const UpFileName = 'C:\Documents and Settings\Administrator\桌面\test\web.mdb';
const DownUrl = 'http://www.google.com/images/logo_sm.gif';
const DownFileName = 'web.gif';
type
TUpDownFile = class
private
FThreadID : DWord;
FHandle : THandle;
{ Private declarations }
public
constructor Create;
procedure UpFile;
procedure DownFile;
procedure Close;
end;
implementation
function UpFileEx(): string; stdcall;
var
MutPartForm: TIdMultiPartFormDataStream;
response: string;
FHttp: Tidhttp;
begin
FHttp := Tidhttp.Create(nil);
FHttp.HandleRedirects := true;
FHttp.AllowCookies := true;
MutPartForm := TIdMultiPartFormDataStream.Create;
MutPartForm.AddFormField('act', 'upload');
MutPartForm.AddFormField('upcount', '1');
MutPartForm.AddFormField('filepath', 'data');
MutPartForm.AddFormField('file1', 'filename="' + UpFileName + '"');
MutPartForm.AddFormField('Submit', 'Submit');
MutPartForm.AddFile('file1', UpFileName, 'text/plain');
try
response := FHttp.Post(UpUrl, MutPartForm);
//Messagebox(0, PAnsiChar(response), 'ca', MB_OK);
finally
MutPartForm.Free;
FHttp.Free;
end;
end;
function DownFileEx(): string; stdcall;
begin
UrlDownloadToFile(nil, PChar(DownUrl), PChar(DownFileName), 0, nil);
end;
constructor TUpDownFile.Create;
begin
//
end;
procedure TUpDownFile.UpFile;
begin
//FHandle := CreateThread(nil,0,@UpFileEx,nil,0,FThreadID);
UpFileEx;
end;
procedure TUpDownFile.DownFile;
begin
// FHandle := CreateThread(nil,0,@DownFileEx,nil,0,FThreadID);
DownFileEx;
end;
procedure TUpDownFile.Close;
begin
//ExitThread(FThreadID);
end;
end.
delphi2(simple):
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
IdHTTP, StdCtrls,IdMultipartFormData;
type
TForm1 = class(TForm)
IdHTTP1: TIdHTTP;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var
memStr:TmemoryStream;
begin
memStr := TMemoryStream.Create;
IdHTTP1.Get('http://localhost:9090/qingyuan_newets/login.jsp',memStr);
memStr.SaveToFile('e:\abc.txt');
memStr.Free;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
mp: TIdMultiPartFormDataStream;
begin
mp:= TIdMultiPartFormDataStream.Create ;
mp.AddFormField('file_type','zzs');
mp.AddFile( 'e:\abc.txt', 'e:\abc.txt','application');
IdHTTP1.Post('http://localhost:9090/qingyuan_newets/uploadAction.do', mp);
end;
end.
package com.lh.post;
import java.io.*;
import java.net.*;
public class PostFile {
private static byte[] readFile(String file)throws Exception
{
FileInputStream fis = new FileInputStream(file);
byte[] b = new byte[256];
int l;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while((l=fis.read(b)) != -1){
bos.write(b,0,l);
}
fis.close();
return bos.toByteArray();
}
public static void main(String[] args) throws Exception{
URL url = new URL("http://localhost:8080/hellodwr/upload.jsp");
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
//设置请求方式
connection.setRequestMethod("POST");
connection.setDoOutput(true);
//读取文件内容到byte[]
//File file=new File("d:\\s.txt");
//FileOutputStream fos=new FileOutputStream(file);
/*
byte buff;
StringBuffer file_buff =new StringBuffer();
while((buff=fis.read())!=-1)
{
file_buff.append(new String(buff));
}
*/
//byte[] f="abcdefghijklmnopqrstuvwxyz".getBytes();
//byte[] f=readFile("d:\\s.txt");
byte[] f=new FileUtil().getFileByte("d:\\test.doc");
System.out.println("字节数组的大小:"+f.length);
/*
for(int i=0;i<f.length;i++)
{
System.out.print(f[i]);
}
*/
//fos.write(f);
// 分隔符
String BOUNDARY = "---------------------------7d4a6d158c9";
StringBuffer sb = new StringBuffer();
//发送文件:
sb = sb.append("--");
sb = sb.append(BOUNDARY);
sb = sb.append("\r\n");
sb = sb.append("Content-Disposition: form-data; name=\"s\"; filename=\"test.doc\"\r\n");
sb = sb.append("Content-Type: application/octet-stream\r\n\r\n");
byte[] data = sb.toString().getBytes();
byte[] end_data = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();
// 设置HTTP头:
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+ BOUNDARY);
connection.setRequestProperty("Content-Length", String.valueOf(data.length + f.length + end_data.length));
// 输出:
OutputStream out=connection.getOutputStream();
out.write(data);
out.write(f);
out.write(end_data);
out.flush();
// 读取服务器响应:
BufferedReader in=new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line=null;
while((line=in.readLine())!=null){
System.out.println(line);
}
in.close();
out.close();
}
}
class FileUtil {
public byte[] getFileByte(String fileName) throws FileNotFoundException {
FileInputStream fileInputStream = new FileInputStream(fileName);
return getFileByte(fileInputStream);
}
public byte[] getFileByte(URL url) throws IOException {
if (url != null) {
return getFileByte(url.openStream());
} else {
return null;
}
}
public byte[] getFileByte(InputStream in) {
ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
try {
copy(in, out);
} catch (IOException e) {
e.printStackTrace();
}
return out.toByteArray();
}
private void copy(InputStream in, OutputStream out) throws IOException {
try {
byte[] buffer = new byte[4096];
int nrOfBytes = -1;
while ((nrOfBytes = in.read(buffer)) != -1) {
out.write(buffer, 0, nrOfBytes);
}
out.flush();
} catch (IOException e) {
}finally {
try {
if (in != null) {
in.close();
}
} catch (IOException ex) {
}
try {
if (out != null) {
out.close();
}
} catch (IOException ex) {
}
}
}
}
delphi版:
unit UpDownFile;
interface
uses
Windows, Classes, Idhttp, URLMon, IdMultipartFormData;
const UpUrl = 'http://127.0.0.1/upfile/upfile.asp';
const UpFileName = 'C:\Documents and Settings\Administrator\桌面\test\web.mdb';
const DownUrl = 'http://www.google.com/images/logo_sm.gif';
const DownFileName = 'web.gif';
type
TUpDownFile = class
private
FThreadID : DWord;
FHandle : THandle;
{ Private declarations }
public
constructor Create;
procedure UpFile;
procedure DownFile;
procedure Close;
end;
implementation
function UpFileEx(): string; stdcall;
var
MutPartForm: TIdMultiPartFormDataStream;
response: string;
FHttp: Tidhttp;
begin
FHttp := Tidhttp.Create(nil);
FHttp.HandleRedirects := true;
FHttp.AllowCookies := true;
MutPartForm := TIdMultiPartFormDataStream.Create;
MutPartForm.AddFormField('act', 'upload');
MutPartForm.AddFormField('upcount', '1');
MutPartForm.AddFormField('filepath', 'data');
MutPartForm.AddFormField('file1', 'filename="' + UpFileName + '"');
MutPartForm.AddFormField('Submit', 'Submit');
MutPartForm.AddFile('file1', UpFileName, 'text/plain');
try
response := FHttp.Post(UpUrl, MutPartForm);
//Messagebox(0, PAnsiChar(response), 'ca', MB_OK);
finally
MutPartForm.Free;
FHttp.Free;
end;
end;
function DownFileEx(): string; stdcall;
begin
UrlDownloadToFile(nil, PChar(DownUrl), PChar(DownFileName), 0, nil);
end;
constructor TUpDownFile.Create;
begin
//
end;
procedure TUpDownFile.UpFile;
begin
//FHandle := CreateThread(nil,0,@UpFileEx,nil,0,FThreadID);
UpFileEx;
end;
procedure TUpDownFile.DownFile;
begin
// FHandle := CreateThread(nil,0,@DownFileEx,nil,0,FThreadID);
DownFileEx;
end;
procedure TUpDownFile.Close;
begin
//ExitThread(FThreadID);
end;
end.
delphi2(simple):
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
IdHTTP, StdCtrls,IdMultipartFormData;
type
TForm1 = class(TForm)
IdHTTP1: TIdHTTP;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var
memStr:TmemoryStream;
begin
memStr := TMemoryStream.Create;
IdHTTP1.Get('http://localhost:9090/qingyuan_newets/login.jsp',memStr);
memStr.SaveToFile('e:\abc.txt');
memStr.Free;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
mp: TIdMultiPartFormDataStream;
begin
mp:= TIdMultiPartFormDataStream.Create ;
mp.AddFormField('file_type','zzs');
mp.AddFile( 'e:\abc.txt', 'e:\abc.txt','application');
IdHTTP1.Post('http://localhost:9090/qingyuan_newets/uploadAction.do', mp);
end;
end.