SoFunction
Updated on 2025-03-09

HttpClient uploads the instance code of files through Post

In the previous project, Java was used to imitate Http Post to send parameters and files. Simple pass parameters or files can be processed accordingly using URLConnection.

However, the project involves passing both ordinary parameters and multiple files (not simply passing XML files). After searching online, I found that it was using HttClient to respond. At first, I tried to pass parameters and files many times but still could not pass them. Later, I found that when using HttpClient, I could not use () to obtain ordinary parameters, but I had to use Upload to operate on the server side.

HttpClient4.2 jar download:/detail/just_szl/4370574

Client code:

import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
/** 
* 
* @author <a href="mailto:just_szl@"> Geray</a> 
* @version 1.0,2012-6-12 
*/ 
public class HttpPostArgumentTest2 { 
//file1 and file2 are in the same folder. filepath is the path specified by this folder.public void SubmitPost(String url,String filename1,String filename2, String filepath){ 
HttpClient httpclient = new DefaultHttpClient(); 
try { 
HttpPost httppost = new HttpPost(url); 
FileBody bin = new FileBody(new File(filepath +  + filename1)); 
FileBody bin2 = new FileBody(new File(filepath +  + filename2)); 
StringBody comment = new StringBody(filename1); 
MultipartEntity reqEntity = new MultipartEntity(); 
("file1", bin);//file1 is the File upload of the request background; attribute("file2", bin2);//file2 is the File upload of the request background; attribute("filename1", comment);//filename1 is a normal parameter for the request background; attribute(reqEntity); 
HttpResponse response = (httppost); 
int statusCode = ().getStatusCode(); 
if(statusCode == HttpStatus.SC_OK){ 
("The server responds normally..."); 
HttpEntity resEntity = (); 
((resEntity));//The tool class that comes with httpclient reads and returns data(()); 
(resEntity); 
} 
} catch (ParseException e) { 
// TODO Auto-generated catch block 
(); 
} catch (IOException e) { 
// TODO Auto-generated catch block 
(); 
} finally { 
try { 
().shutdown(); 
} catch (Exception ignore) { 
} 
} 
} 
/** 
* @param args 
*/ 
public static void main(String[] args) { 
// TODO Auto-generated method stub 
HttpPostArgumentTest2 httpPostArgumentTest2 = new HttpPostArgumentTest2(); 
("http://127.0.0.1:8080/demo/", 
"","","D://test"); 
} 
} 

Server code:

public void receiveData(HttpServletRequest request, HttpServletResponse response) throws AppException{ 
PrintWriter out = null; 
("text/html;charset=UTF-8"); 
Map map = new HashMap(); 
FileItemFactory factory = new DiskFileItemFactory(); 
ServletFileUpload upload = new ServletFileUpload(factory); 
File directory = null; 
List<FileItem> items = new ArrayList(); 
try { 
items = (request); 
// Get all the filesIterator<FileItem> it = (); 
while (()) { 
FileItem fItem = (FileItem) (); 
String fName = ""; 
Object fValue = null; 
if (()) { // Value of a normal text boxfName = (); 
// fValue = (); 
fValue = ("UTF-8"); 
(fName, fValue); 
} else { // Get the value of the uploaded filefName = (); 
fValue = (); 
(fName, fValue); 
String name = (); 
if(name != null && !("".equals(name))) { 
name = (() + 1); 
// String stamp = (); 
String timestamp_Str = (); 
directory = new File("d://test"); 
(); 
String filePath = ("d://test")+ timestamp_Str+  + name; 
(fName + "FilePath", filePath); 
InputStream is = (); 
FileOutputStream fos = new FileOutputStream(filePath); 
byte[] buffer = new byte[1024]; 
while ((buffer) > 0) { 
(buffer, 0, ); 
} 
(); 
(); 
(fName + "FileName", name); 
} 
} 
} 
} catch (Exception e) { 
("There was an error reading the http request attribute value!"); 
// (); 
("Error reading the http request attribute value"); 
} 
// Data processingtry { 
out = (); 
("{success:true, msg:'Received successfully'}"); 
(); 
} catch (IOException e) { 
(); 
} 
} 

The above is the example code of HttpClient uploading files through Post introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!