Java HTTPClient File Upload Server - java

edit: solved below
So I need to create a server that can be used to upload files to and then serve those files back upon request. After some research it seemed like using HttpClient on the client side and FileUpload on the server side with a Tomcat servlet would be a good way to accomplish this task. After pulling from various example sources I have come up with the following code for the client:
import java.io.File;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.HttpClientBuilder;
public class Client {
static String url = "http://localhost:8080";
public static void main(String[] args) throws Exception {
File file = new File("2048.zip");
FileBody fileBody = new FileBody(file, ContentType.DEFAULT_BINARY);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addPart("file", fileBody);
HttpEntity entity = builder.build();
HttpPost request = new HttpPost(url);
request.setEntity(entity);
HttpClient client = HttpClientBuilder.create().build();
try {
client.execute(request);
} catch (IOException e) {
e.printStackTrace();
}
//list_files();
}
}
And this for the servlet code:
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
public class FileServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (isMultipart) {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
try {
List items = upload.parseRequest(request);
Iterator iterator = items.iterator();
while (iterator.hasNext()) {
FileItem item = (FileItem) iterator.next();
if (!item.isFormField()) {
String fileName = item.getName();
String root = getServletContext().getRealPath("/");
File path = new File(root);
if (!path.exists()) {
boolean status = path.mkdirs();
}
File uploadedFile = new File(path + "/" + fileName);
System.out.println(uploadedFile.getAbsolutePath());
item.write(uploadedFile);
}
}
} catch (FileUploadException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
This in the web-xml:
<servlet>
<servlet-name>fileservlet</servlet-name>
<servlet-class>servlet.FileServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>fileservlet</servlet-name>
<url-pattern>/fileservlet</url-pattern>
</servlet-mapping>
When I try to run the client I just get hit with these errors until it times out.
Dec 04, 2017 4:08:34 PM org.apache.http.impl.execchain.RetryExec execute
INFO: I/O exception (java.net.SocketException) caught when processing request to {}->http://localhost:8080: Connection reset by peer: socket write error
Dec 04, 2017 4:08:34 PM org.apache.http.impl.execchain.RetryExec execute
INFO: Retrying request to {}->http://localhost:8080
This is my first time working with servlets and Tomcat so I'm unsure if there's a configuration issue in eclipse or an actual issue with the code.
Edit: Okay so for the first time I've managed to get the file to send through the client and be saved by the server. The first thing I had to do was ensure that the "url" matched the correct servlet extension. When I launched the servlet in eclipse it automatically opened http://localhost:8080/FileServlet/FileServlet so this is what I used in the client when sending the file. I also uncommented a part of the servers.xml file having to do with thread pool.
<Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
maxThreads="150" minSpareThreads="4"/>
Also I used the code straight from the HttpClient example page for multipart sending.

Related

File name missing in REST API via HTTP Post

I am doing a program which can take the file as input and send http://IPaddress:port_number/etc via REST API HTTP Post method, it will return me my file information in JSON format but in the return of my JSON data, I found out my file name and file type were missing. I only have two program doing the job ,one is sending the file to my server which install in VMware and one is receiving the JSON data from my server, my server only installed a malware scanning engine only Here is my JSON data return from my server
{
   "data_id":"7860ee33d8f14f8f931860dcf97d69a2",
   "file_info":{
      "display_name":"",
      "file_size":289,
      "file_type":"Not available",
      "file_type_description":"Not available",
      "md5":"b294bdea53f3a038d263d8b2e7cafbbc",
      "sha1":"b869fd379cb9743b6c0994494f3ea91a133622cd",
      "sha256":"0f88b399aa65ad75a4e0d1d03acfd31731c68ea54d5f4f5053d6904d192e5e22",
      "upload_timestamp":"2016-11-21T09:48:58.675Z" },
I had tested several code in online but it's also fail to display the file name in the JSON data, display_name will be the file name for my file. May I know is it because some information being miss out in the header and how do I include the file name during the return JSON data ? Here is my java source code
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.activation.MimetypesFileTypeMap;
import java.io.*;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;
public class SentFile {
public static void main(String [] args) throws ClientProtocolException, IOException
{
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://192.168.0.25:8008/file");
// File file = new File("testScanFile.txt");
//FileInputStream fileInputStream = new FileInputStream(file);
FileBody bin = new FileBody(new File("testScanFile.txt"));
// String mimeType = new MimetypesFileTypeMap().getContentType("testScanFile.txt");
HttpEntity reqEntity = MultipartEntityBuilder.create()
// .addBinaryBody("bin", new File("testScanFile.txt"),ContentType.APPLICATION_JSON,"testScanFile.txt").build();
.addPart("display_name", bin)
// .addPart("file",bin);
.build();
// post.addHeader("content-type","application/json");
// post.addHeader("Accept","application/json");
post.setEntity(reqEntity);
//InputStream is = new FileInputStream(file);
// post.setEntity(new FileEntity(file));
HttpResponse response = client.execute(post);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
// System.out.println(line);
PrintStream ps = new PrintStream(new FileOutputStream("data_id.txt"));
ps.print(line);
ps.close();
}
}
}

Uploading a file and Appending its contents to an EXISTING text file

I want to copy the contents of an uploaded file to a text file on my local.
The important thing is- I already have a file on my system named "Data" and I want to append the contents of uploaded file in "Data".
I have successfully copied the contents of the uploaded file but facing issue in appending the data.
Below is my Servlet file:
package pack;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class FileUploadHandler extends HttpServlet {
private final String UPLOAD_DIRECTORY = "D:/Data Repository.txt";
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//process only if its multipart content
if(ServletFileUpload.isMultipartContent(request)){
try {
List<FileItem> multiparts = new ServletFileUpload(
new DiskFileItemFactory()).parseRequest(request);
for(FileItem item : multiparts){
if(!item.isFormField()){
item.write(new File(UPLOAD_DIRECTORY));
}
}
//File uploaded successfully
request.setAttribute("message", "File Uploaded Successfully");
} catch (Exception ex)
{
request.setAttribute("message", "File Upload Failed due to " + ex);
}
}else{
request.setAttribute("message",
"Sorry this Servlet only handles file upload request");
}
request.getRequestDispatcher("/result.jsp").forward(request, response);
}
}
Can something be added or modified in the below statement so that it would append the data to the existing file rather than creating a new file every time and copying its content?
item.write(new File(UPLOAD_DIRECTORY));

Verify header before request receive in tomcat

I need verify header before receive the request. I found that tomcat valve can help in it. I follow these steps but valve is not called:
make a maven project and do this code in it.
package cz.ValveTest;
import java.io.IOException;
import java.util.Enumeration;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import org.apache.catalina.connector.Request;
import org.apache.catalina.connector.Response;
import org.apache.catalina.valves.ValveBase;
public class ProcessingValve extends ValveBase {
private static final Logger logger = Logger.getLogger(ProcessingValve.class.getName());
#Override
public void invoke(Request request, Response response) throws IOException,
ServletException {
HttpServletRequest httpServletRequest = request.getRequest();
Enumeration<String> headerNames = httpServletRequest.getHeaderNames();
logger.info("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
while (headerNames.hasMoreElements()) {
String header = headerNames.nextElement();
logger.log(Level.INFO, "Header --> {0} Value --> {1}", new Object[]{header, httpServletRequest.getHeader(header)});
}
getNext().invoke(request, response);
}
}
make jar and put jar inside tomcat/lib folder
add this line in server.xml
<valve className="cz.ValveTest.ProcessingValve"/>
restart tomcat.
Now I hit my web service with header:
Expect : 100-continue
but using this configuration and code valve is not called on http hit.If any one knew why tomcat valve is not called please help.
The tags in server.xml are case sensitive.
So try this :
<Valve className="cz.ValveTest.ProcessingValve"/>

how to upload excel sheet in servlet using browser

I want to upload my excel sheet in my local system using servlet API but i am getting exception
java.io.FileNotFoundException: D:\Core-Java_Eclipse.metadata.plugins
\org.eclipse.wst.server.core\tmp0\wtpwebapps\UploadMenu\uploaded (Access is denied)
here i am using two jar 1. commons-fileupload-1.2.1.jar 2. commons-io-1.4.jar
i dont want to use third party implementation jar
Please see my code which is written in servlet
import java.io.*;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class UploadServlet extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
String ctxPath=request.getRealPath("/");
File dir=new File(ctxPath,"uploaded");
if(!dir.exists()){
dir.mkdir();
}
Writer out=response.getWriter();
boolean uploadData=ServletFileUpload.isMultipartContent(request);
if(uploadData){
DiskFileItemFactory factory=new DiskFileItemFactory();
factory.setSizeThreshold(1024*1024*10);
factory.setRepository(new File("c:\\temp"));
ServletFileUpload upload=new ServletFileUpload(factory);
upload.setSizeMax(1024*1024*50);
try{
List<FileItem> fileItems=upload.parseRequest(request);
Iterator<FileItem> i=fileItems.iterator();
while(i.hasNext()){
FileItem fi=i.next();
if(!fi.isFormField()){
String fieldName=fi.getFieldName();
String fileName=fi.getName();
String contentType=fi.getContentType();
boolean isInMemory=fi.isInMemory();
long sizeInByte=fi.getSize();
StringTokenizer tok=new
StringTokenizer(fileName,"/");
String fileToWrite="";
while(tok.hasMoreTokens()){
fileToWrite=tok.nextToken();
}
File file=new File(dir,fileToWrite);
fi.write(file);
}
}
}catch(Exception e){
e.printStackTrace();
}
out.write("<h1>File Uploaded in <br/>"+dir.getAbsolutePath());
}else{
out.write("No File Uploaded");
}
}
}
please tell me how to resolve my problem and how to upload this file is user
define directry means (D:// menu) folder
thanx is Advance

Automate HTML form submission using Java to find grocery hours

I'm trying to automate form submission using Java to get the hours of a grocery store here:
www.giantfood.com
I've posted the query and the hidden miles and storeType fields of the form, but my output.html is just the original web header and footer with an error message in the body. What am I doing wrong?
import java.io.*;
import java.net.*;
public class PostHTML
{
public static void main(String[] args)
{
try
{
URL url = new URL( "http://www.giantfood.com/our_stores/locator/store_search.htm" );
HttpURLConnection hConnection = (HttpURLConnection)
url.openConnection();
HttpURLConnection.setFollowRedirects( true );
hConnection.setDoOutput( true );
hConnection.setRequestMethod("POST");
PrintStream ps = new PrintStream( hConnection.getOutputStream() );
ps.print("groceryStoreAddress=20814&groceryStoreMiles=10&storeType=GROCERY");
ps.close();
hConnection.connect();
if( HttpURLConnection.HTTP_OK == hConnection.getResponseCode() )
{
InputStream is = hConnection.getInputStream();
OutputStream os = new FileOutputStream("output.html");
int data;
while((data=is.read()) != -1)
{
os.write(data);
}
is.close();
os.close();
hConnection.disconnect();
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
UPDATE
Thanks! Using &'s worked. I'm trying to use HttpClient but I'm getting another error now:
package clientwithresponsehandler;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
/**
* This example demonstrates the use of the {#link ResponseHandler} to simplify
* the process of processing the HTTP response and releasing associated resources.
*/
public class ClientWithResponseHandler {
public static void main(String[] args) throws Exception {
HttpClient httpclient = new DefaultHttpClient();
try {
HttpPost httpost = new HttpPost("http://www.giantfood.com/our_stores/locator/store_search.htm");
System.out.println("executing request " + httpost.getURI());
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("groceryStoreAddress", "20878"));
nvps.add(new BasicNameValuePair("groceryStoreMiles", "10"));
nvps.add(new BasicNameValuePair("storeType", "GROCERY"));
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
// Create a response handler
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = httpclient.execute(httpost, responseHandler);
System.out.println("----------------------------------------");
System.out.println(responseBody);
System.out.println("----------------------------------------");
} finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
}
}
Output:
run:
executing request http://www.giantfood.com/our_stores/locator/store_search.htm
Exception in thread "main" org.apache.http.client.HttpResponseException: Moved Temporarily
at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:67)
at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:55)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:945)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:919)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:910)
at clientwithresponsehandler.ClientWithResponseHandler.main(ClientWithResponseHandler.java:39)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)
I don't understand the Moved Temporarily error.
try to use
ps.print("groceryStoreAddress=20814&groceryStoreMiles=10&storeType=GROCERY")
instead
BTW, it's easier to use some http-library, like Apache HttpClient
Solved the Moved Temporarily by learning about HTML Redirects:
Httpclient 4, error 302. How to redirect?

Categories