Package TOOLS;
Import Java.io.BufferedReader;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import Java.io.OutputStream;
Import java.net.InetAddress;
Import Java.net.Socket;
Import Java.net.URL;
Import Java.util.Map;
Import Android.util.Log;
/**
* uploaded files
*/
public class Formfile {
Private final static String Logkey = "Formfile";
/** upload the file data * *
Private byte[] data;
Private InputStream instream;
private file file;
/** File Name * *
Private String Filname;
/** Request Parameter Name * *
Private String parametername;
/** Content Type * *
Private String ContentType = "Application/octet-stream";
/**
*
* @param filname
* File name
* @param data
* uploaded file data
* @param parametername
* Parameter
* @param contentType
* Content Type
*/
Public Formfile (String filname, byte[] data, String parametername,
String ContentType) {
This.data = data;
This.filname = Filname;
This.parametername = parametername;
if (ContentType!= null)
This.contenttype = ContentType;
}
/**
*
* @param filname
* File name
* @param file
* uploaded files
* @param parametername
* Parameter
* @param contentType
* Content Content Type
*/
Public Formfile (String filname, File file, string parametername,
String ContentType) {
This.filname = Filname;
This.parametername = parametername;
This.file = file;
try {
This.instream = new FileInputStream (file);
catch (FileNotFoundException e) {
E.printstacktrace ();
}
if (ContentType!= null)
This.contenttype = ContentType;
}
Public File GetFile () {
return file;
}
Public InputStream Getinstream () {
return instream;
}
Public byte[] GetData () {
return data;
}
Public String Getfilname () {
return filname;
}
public void Setfilname (String filname) {
This.filname = Filname;
}
Public String Getparametername () {
return parametername;
}
public void SetParameterName (String parametername) {
This.parametername = parametername;
}
Public String getContentType () {
return contentType;
}
public void setContentType (String contentType) {
This.contenttype = ContentType;
}
Private String Opertype=null;
public void Setoper (String opertype) {
This. Opertype=opertype;
}
public Boolean post (String path, map<string, string> params)
Throws Exception {
Final String boundary = "--------------7da2137580612"; Data separator Line
Final String endline = "--" + Boundary + "--\r\n";//Data End flag
int filedatalength = 0;
Gets the total length of the file type data
StringBuilder Fileexplain = new StringBuilder ();
Fileexplain.append ("--");
Fileexplain.append (boundary);
Fileexplain.append ("\ r \ n");
Fileexplain.append ("content-disposition:form-data;name=\")
+ getparametername () + "\"; filename=\ "" + getfilname ()
+ "\ \ r \ n");
Fileexplain.append ("Content-type:" + getcontenttype () + "\r\n\r\n");
Fileexplain.append ("\ r \ n");
Filedatalength + + fileexplain.length ();
if (Getinstream ()!= null) {
Filedatalength + + getFile (). Length ();
} else {
Filedatalength + = GetData (). length;
}
StringBuilder textentity = new StringBuilder ();
For (map.entry<string, string> entry:params.entrySet ()) {//Construct Entity data for text type parameters
Textentity.append ("--");
Textentity.append (boundary);
Textentity.append ("\ r \ n");
Textentity.append ("Content-disposition:form-data; Name=\ ""
+ entry.getkey () + "\ \r\n\r\n");
Textentity.append (Entry.getvalue ());
Textentity.append ("\ r \ n");
}
LOG.V (Logkey, textentity.tostring ());
Calculates the total length of the Entity data transmitted to the server
int datalength = Textentity.tostring (). GetBytes (). length
+ Filedatalength + endline.getbytes (). length;
URL url = new URL (path);
LOG.V (Logkey, url.tostring ());
int port = Url.getport () = = 1? 80:url.getport ();
Socket socket = new Socket (Inetaddress.getbyname (Url.gethost ()), port);
OutputStream OutStream = Socket.getoutputstream ();
The following completes the sending of the HTTP request headers
String Requestmethod = "POST" + url.getpath () + "?" +opertype + "http/1.1\r\n";
LOG.V (Logkey, Requestmethod);
Outstream.write (Requestmethod.getbytes ());
String accept = "Accept:image/gif, Image/jpeg, Image/pjpeg, Image/pjpeg, Application/x-shockwave-flash, application/ Xaml+xml, Application/vnd.ms-xpsdocument, APPLICATION/X-MS-XBAP, Application/x-ms-application, application/ Vnd.ms-excel, Application/vnd.ms-powerpoint, Application/msword, */*\r\n ";
Outstream.write (Accept.getbytes ());
String language = "accept-language:zh-cn\r\n";
Outstream.write (Language.getbytes ());
String contenttype = "Content-type:multipart/form-data;" Boundary= "
+ boundary + "\ r \ n";
Outstream.write (Contenttype.getbytes ());
String contentlength = "Content-length:" + datalength + "\ r \ n";
Outstream.write (Contentlength.getbytes ());
String alive = "connection:keep-alive\r\n";
Outstream.write (Alive.getbytes ());
String host = "Host:" + url.gethost () + ":" + port + "\ r \ n";
Outstream.write (Host.getbytes ());
Write a carriage return break based on the HTTP protocol after the HTTP request header has been written.
Outstream.write ("\ r \ n". GetBytes ());
Send all text-type Entity Data
Outstream.write (Textentity.tostring (). GetBytes ());
Send Entity data for all file types
StringBuilder fileentity = new StringBuilder ();
Fileentity.append ("--");
Fileentity.append (boundary);
Fileentity.append ("\ r \ n");
Fileentity.append ("content-disposition:form-data;name=\")
+ getparametername () + "\"; filename=\ "" + getfilname ()
+ "\ \ r \ n");
Fileentity.append ("Content-type:" + getcontenttype () + "\r\n\r\n");
Outstream.write (Fileentity.tostring (). GetBytes ());
if (Getinstream ()!= null) {
byte[] buffer = new byte[1024];
int len = 0;
while (len = Getinstream (). Read (buffer, 0, 1024))!=-1) {
Outstream.write (buffer, 0, Len);
}
Getinstream (). Close ();
} else {
Outstream.write (GetData (), 0, GetData (). length);
}
Outstream.write ("\ r \ n". GetBytes ());
//below sends a data end sign indicating that the data has been closed
Outstream.write (Endline.getbytes ());
BufferedReader reader = new BufferedReader (new InputStreamReader (Socket.getinputstream ()));
if (Reader.readline (). IndexOf ("200") = = 1) {//Read the data returned by the Web server. Determine if the request code is 200, if not 200, the request failed
return false;
}
Outstream.flush () ;
outstream.close ();
reader.close ();
socket.close ();
return true;
}
}
//test code
File IconFile = new file ("File path");
String url= "Htttp://192.168.1.101:8080/app/initservlet";
Map<string, string> Map = new hashmap<string, string> ();//form content
Map.put ("name", "blog");
if (iconfile!= null) {
Formfile uploadfile = new Formfile (Iconfile.getname (),
IconFile, "IconFile", "image/jpeg");
Uploadfile.setoper ("action=insertusr");//insert on URL? action=insertusr
try {
Boolean isok = uploadfile.post (URL, map);
catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}