Atihska Atihska - 1 year ago 205
Java Question

FileNotFoundException for in aws lambda test console

I have an AWS lambda sample, created using AWS Toolkit for eclipse. I added a file in the project from eclipse. I am also then uploading using right click project->Amazon Web Services -> Upload

But when I test on aws console, it gives me filenotfound for

Please help!

Here is my project structure: I get error at line 33 telling that file not found. enter image description here

here is my lambda function:


public class LambdaFunctionHandler implements RequestHandler {

public void handleRequest(String input, Context context) {

context.getLogger().log("Input: " + input);

try {
new PreviewService().GetPreview(input);
} catch (Exception e) {
// TODO Auto-generated catch block
return null;

public void GetPreview(String downloadUrl) throws Exception{

input = new FileInputStream(""); //ERROR HERE: FileNotFoundException by aws lambda when testing on aws lambda console.


//Download File
downloadFileFromUrl(new URL(downloadUrl));

return null;


public void downloadFileFromUrl(URL downloadUrl)throws Exception{

FileUtils.copyURLToFile(downloadUrl, new File("<filepath>"));



public void uploadFileToServer(String filePath) throws Exception

String fileExternalRefId = "id";
String param = getProperty("param");
URL uploadUrl = new URL(getProperty("uploadurl"));

File contents = new File("<filepath>");

String boundary = Long.toHexString(System.currentTimeMillis());

String CRLF = "\r\n"; //Line Separator required by multipart/form-data

URLConnection connection = uploadUrl.openConnection();
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
connection.addRequestProperty("file_name", contents.getName());
connection.addRequestProperty("id", fileId);

OutputStream output = connection.getOutputStream();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, "UTF-8"), true);
) {

//Send headers/params
writer.append("--" + boundary).append(CRLF);
writer.append("Content-Disposition: form-data; name=\"param\"").append(CRLF);
writer.append("Content-Type: application/xml; charset=UTF-8").append(CRLF);

//Send contents
writer.append("--" + boundary).append(CRLF);
writer.append("Content-Disposition: form-data; name=\"file-content\"; filename=\"" + contents.getName() + "\"").append(CRLF);
writer.append("Content-Type: application/xml; charset=UTF-8").append(CRLF);

Files.copy(contents.toPath(), output);
//IOUtils.copy(in, output);
writer.append(CRLF).flush();//It indicates end of boundary

writer.append("--" + boundary + "--").append(CRLF).flush();

int responseCode = ((HttpURLConnection) connection).getResponseCode();

if(responseCode == 200)
String viewUrl = props.getProperty("url")


public String getProperty(String key)
return props.getProperty(key);


Here is my that looks like


Answer Source

I have little experience with AWS, but when you work with java Files or FileInputStreams must use the file path and you are using just the file name.

I think your code should be:

input = new FileInputStream("/[appDeployPath]/");

Maybe a better approach is to use: