function uploadSingleFile ( file) {
var formData = new FormData ( ) ;
formData. append ( "file" , file) ;
var xhr = new XMLHttpRequest ( ) ;
xhr. open ( "POST" , "/uploadFile" ) ;
xhr. onload = function ( ) {
console. log ( xhr. responseText) ;
var response = JSON . parse ( xhr. responseText) ;
if ( xhr. status == 200 ) {
singleFileUploadError. style. display = "none" ;
singleFileUploadSuccess. innerHTML = "<p>File Uploaded Successfully.</p><p>DownloadUrl : <a href='" + response. fileDownloadUri + "' target='_blank'>" + response. fileDownloadUri + "</a></p>" ;
singleFileUploadSuccess. style. display = "block" ;
} else {
singleFileUploadSuccess. style. display = "none" ;
singleFileUploadError. innerHTML = ( response && response. message) || "Some Error Occurred" ;
}
}
xhr. send ( formData) ;
}
package com. example. filedemo. controller;
import java. io. ByteArrayOutputStream;
import java. io. File;
import java. io. FileInputStream;
import java. io. InputStream;
import java. io. OutputStream;
import java. net. HttpURLConnection;
import java. net. URL;
import java. util. HashMap;
import java. util. LinkedList;
import java. util. List;
import java. util. Map;
import java. util. UUID;
public class HttpMultipart
{
public static void main ( String[ ] args) throws Exception
{
String link = "http://127.0.0.1:8080/uploadFile" ;
HttpMultipart hm = new HttpMultipart ( link) . addParameter ( "file" , new File ( "G:\\work\\20200819\\servletapi_jb51\\servletapi\\servlet-api.jar" ) ) . addParameter ( "other" , "somevalue" ) ;
String result = new String ( hm. send ( ) ) ;
hm. clearParameter ( ) ;
System. out. println ( result) ;
}
private URL url;
private String boundary = "--------" + UUID. randomUUID ( ) . toString ( ) . replace ( "-" , "" ) ;
private Map< String, List< String> > textParams = new HashMap < String, List< String> > ( ) ;
private Map< String, List< File> > fileParams = new HashMap < String, List< File> > ( ) ;
public HttpMultipart ( String url) throws Exception
{
this . url = new URL ( url) ;
}
protected Map< String, List< File> > getFileParams ( )
{
return fileParams;
}
protected Map< String, List< String> > getTextParams ( )
{
return textParams;
}
public HttpMultipart addParameter ( String name, String value)
{
List< String> list = textParams. get ( name) ;
if ( list == null)
{
list = new LinkedList < String> ( ) ;
list. add ( value) ;
textParams. put ( name, list) ;
}
else
{
list. add ( value) ;
}
return this ;
}
public HttpMultipart addParameter ( String name, File value)
{
List< File> list = fileParams. get ( name) ;
if ( list == null)
{
list = new LinkedList < File> ( ) ;
list. add ( value) ;
fileParams. put ( name, list) ;
}
else
{
list. add ( value) ;
}
return this ;
}
public HttpMultipart removeTextParameter ( String name)
{
textParams. remove ( name) ;
return this ;
}
public HttpMultipart removeTextParameter ( String name, int index)
{
List< String> list = textParams. get ( name) ;
if ( list != null)
{
if ( list. size ( ) > index)
{
list. remove ( index) ;
}
if ( list. size ( ) == 0 )
{
textParams. remove ( name) ;
}
}
return this ;
}
public HttpMultipart removeFileParameter ( String name)
{
fileParams. remove ( name) ;
return this ;
}
public HttpMultipart removeFileParameter ( String name, int index)
{
List< File> list = fileParams. get ( name) ;
if ( list != null)
{
if ( list. size ( ) > index)
{
list. remove ( index) ;
}
if ( list. size ( ) == 0 )
{
fileParams. remove ( name) ;
}
}
return this ;
}
public HttpMultipart clearTextParameter ( )
{
textParams. clear ( ) ;
return this ;
}
public HttpMultipart clearFileParameter ( )
{
fileParams. clear ( ) ;
return this ;
}
public HttpMultipart clearParameter ( )
{
textParams. clear ( ) ;
fileParams. clear ( ) ;
return this ;
}
public byte [ ] send ( ) throws Exception
{
HttpURLConnection connection = ( HttpURLConnection) url. openConnection ( ) ;
long length = getLength ( ) ;
connection. setDoOutput ( true ) ;
connection. setUseCaches ( false ) ;
connection. setConnectTimeout ( 10000 ) ;
connection. setRequestMethod ( "POST" ) ;
connection. setRequestProperty ( "Content-Type" , "multipart/form-data; boundary=" + boundary) ;
connection. setRequestProperty ( "Content-Length" , length + "" ) ;
connection. setChunkedStreamingMode ( 65536 ) ;
connection. connect ( ) ;
OutputStream o = connection. getOutputStream ( ) ;
writeFileParams ( o) ;
writeTextParams ( o) ;
paramsEnd ( o) ;
InputStream in = connection. getInputStream ( ) ;
ByteArrayOutputStream out = new ByteArrayOutputStream ( ) ;
transfer ( in, out, 1024 ) ;
out. close ( ) ;
in. close ( ) ;
connection. disconnect ( ) ;
return out. toByteArray ( ) ;
}
private long getParamLength ( String name, String value) throws Exception
{
return 49 + boundary. length ( ) + name. length ( ) + value. getBytes ( ) . length;
}
private long getParamLength ( String name, File value) throws Exception
{
return 102 + boundary. length ( ) + name. length ( ) + value. getName ( ) . getBytes ( ) . length + value. length ( ) ;
}
private long getLength ( ) throws Exception
{
long length = 0 ;
for ( String name : textParams. keySet ( ) )
{
List< String> list = textParams. get ( name) ;
for ( String value : list)
{
length = length + getParamLength ( name, value) ;
}
}
for ( String name : fileParams. keySet ( ) )
{
List< File> list = fileParams. get ( name) ;
for ( File value : list)
{
length = length + getParamLength ( name, value) ;
}
}
length = length + boundary. length ( ) + 8 ;
return length;
}
private void writeTextParams ( OutputStream o) throws Exception
{
for ( String name : textParams. keySet ( ) )
{
List< String> list = textParams. get ( name) ;
for ( String value : list)
{
o. write ( new StringBuffer ( ) . append ( "--" ) . append ( boundary) . append ( "\r\nContent-Disposition: form-data; name=\"" ) . append ( name) . append ( "\"\r\n\r\n" ) . append ( value) . append ( "\r\n" ) . toString ( )
. getBytes ( ) ) ;
}
}
}
private void writeFileParams ( OutputStream o) throws Exception
{
for ( String name : fileParams. keySet ( ) )
{
List< File> list = fileParams. get ( name) ;
for ( File value : list)
{
o. write ( new StringBuffer ( ) . append ( "--" ) . append ( boundary) . append ( "\r\nContent-Disposition: form-data; name=\"" ) . append ( name) . append ( "\"; filename=\"" ) . append ( value. getName ( ) )
. append ( "\"\r\nContent-Type: application/octet-stream\r\n\r\n" ) . toString ( ) . getBytes ( ) ) ;
writeFile ( value, o) ;
o. write ( "\r\n" . getBytes ( ) ) ;
}
}
}
private void writeFile ( File f, OutputStream o) throws Exception
{
InputStream in = new FileInputStream ( f) ;
transfer ( in, o) ;
in. close ( ) ;
}
private void paramsEnd ( OutputStream o) throws Exception
{
o. write ( new StringBuffer ( ) . append ( "--" ) . append ( boundary) . append ( "--\r\n\r\n" ) . toString ( ) . getBytes ( ) ) ;
}
public static void transfer ( InputStream in, OutputStream out) throws Exception
{
transfer ( in, out, 65536 ) ;
}
public static void transfer ( InputStream in, OutputStream out, int bufSize) throws Exception
{
byte [ ] b = new byte [ bufSize] ;
int n;
while ( ( n = in. read ( b) ) != - 1 )
{
out. write ( b, 0 , n) ;
}
b = null;
}
}