Import Java.io.DataOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import java.net.HttpURLConnection;
Import java.net.MalformedURLException;
Import Java.net.URL;
Import Java.util.Iterator;
Import Java.util.Map;
Import Java.util.UUID;
Import Org.apache.log4j.Logger;
Import Org.springframework.web.multipart.MultipartFile;
public class Uploadutil
{
private static Uploadutil Uploadutil;
Private static final String boundary = Uuid.randomuuid (). toString (); Boundary identification
Randomly generated
private static final String PREFIX = "--";
private static final String line_end = "\ r \ n";
private static final String Content_Type = "Multipart/form-data"; Content Type
private static final Logger Logger = Logger.getlogger (Uploadutil.class);
/**
* Single sample mode get Upload Tool class
*
* @return
*/
public static Uploadutil getinstance ()
{
if (null = = Uploadutil)
{
Uploadutil = new Uploadutil ();
}
return uploadutil;
}
private static int readtimeout = 1000 * 1000; Read timeout
private static int connecttimeout = 1000 * 1000; Timeout period
/***
* How long is the request to be used
*/
private static int requesttime = 0;
private static final String CHARSET = "Utf-8"; Set encoding
/***
* Upload Successful
*/
public static final int upload_success_code = 1;
/**
* File does not exist
*/
public static final int upload_file_not_exists_code = 2;
/**
* Server Error
*/
public static final int upload_server_error_code = 3;
protected static final int what_to_upload = 1;
protected static final int what_upload_done = 2;
/**
* Upload Files
*/
public static string UploadFile (Multipartfile file, String Filekey, String Requesturl, map<string, string> param)
{
Logger.info ("Image upload request Address:" +requesturl);
String result = null;
RequestTime = 0;
Long requesttime = System.currenttimemillis ();
Long responsetime = 0;
Try
{
URL url = new URL (requesturl);
HttpURLConnection conn = (httpurlconnection) url.openconnection ();
Conn.setreadtimeout (readtimeout);
Conn.setconnecttimeout (connecttimeout);
Conn.setdoinput (TRUE);//Allow input stream
Conn.setdooutput (TRUE);//Allow output stream
Conn.setusecaches (false);//Do not allow caching
Conn.setrequestmethod ("POST"); Request Method
Conn.setrequestproperty ("Charset", Charset);//Set Encoding
Conn.setrequestproperty ("Connection", " Keep-alive ");
Conn.setrequestproperty ("User-agent", "mozilla/4.0" (compatible; MSIE 6.0; Windows NT 5.1; SV1) ");
Conn.setrequestproperty ("Content-type", Content_Type + "; boundary=" + boundary);
/**
* When the file is not empty, package and upload the file
*/
DataOutputStream dos = new DataOutputStream (Conn.getoutputstream ());
StringBuffer SB = null;
String params = "";
/***
* The following is for uploading parameters
*/
if (param! = null && param.size () > 0)
{
Iterator<string> it = Param.keyset (). iterator ();
while (It.hasnext ())
{
SB = null;
SB = new StringBuffer ();
String key = It.next ();
String value = Param.get (key);
Sb.append (PREFIX). Append (Boundary). Append (Line_end);
Sb.append ("Content-disposition:form-data; Name=\ ""). Append (Key). Append ("\" "). Append (Line_end). Append (Line_end);
Sb.append (value). Append (Line_end);
params = sb.tostring ();
Logger.error (key + "=" + params + "# #");
Dos.write (Params.getbytes ());
}
}
SB = null;
params = null;
SB = new StringBuffer ();
/**
* Note here: The value in name is the server side need key only this key can get the corresponding file
* filename is the name of the file, including the suffix name, such as: abc.png
*/
Sb.append ( PREFIX). Append (Boundary). Append (Line_end);
Sb.append ("Content-disposition:form-data; Name=\ "" + Filekey + "\"; Filename=\ "" + file.getoriginalfilename () + "\" "+ line_end);
Sb.append ("content-type:image/pjpeg" + line_end);//The Content-type configured here is important
//, for the type of server-side identification file
Sb.append ( Line_end);
params = sb.tostring ();
SB = null;
Dos.write (Params.getbytes ());
/** Upload file */
InputStream is = File.getinputstream ();
byte[] bytes = new byte[1024];
int len = 0;
while (len = is.read (bytes))! =-1)
{
Dos.write (bytes, 0, len);
}
Is.close ();
Dos.write (Line_end.getbytes ());
Byte[] End_data = (PREFIX + boundary + PREFIX + line_end). GetBytes ();
Dos.write (End_data);
Dos.flush ();
//
Dos.write (Tempoutputstream.tobytearray ());
/**
* Get response Code 200 = Success When response is successful, get response stream
*/
int res = Conn.getresponsecode ();
Logger.info ("Image Interface return code:" +res);
ResponseTime = System.currenttimemillis ();
RequestTime = (int) ((responsetime-requesttime)/1000);
if (res = = 200)
{
InputStream input = Conn.getinputstream ();
StringBuffer sb1 = new StringBuffer ();
int SS;
while (ss = Input.read ())! =-1)
{
Sb1.append ((char) SS);
}
result = Sb1.tostring ();
}
}
catch (Malformedurlexception e)
{
Logger.error ("file read and write exception", e);
}
catch (IOException E)
{
Logger.error ("file read and write exception", e);
}
return result;
}
/**
* Get Upload usage time
*
* @return
*/
public static int Getrequesttime ()
{
return requesttime;
}
}
Uploading files (written by colleagues)