Elvira
Elvira

Reputation: 1440

Nullpointer exception when saving file Java

Hello I'm having some problems with writing a image file to a map in Tomcat.

In the JSP page I'm having this code:

<div class="patterninfo">
        <!-- file upload -->
        <input type="file" name="file" />
        <!-- einde fileupload -->
        <input type="submit" name="button" value="Add new pattern" class="button">
</div>

In the EditorServlet I'm having this part of code to write the file to tomcat map (tomcat 8-0-18).

Part FilePart = req.getPart("file");
InputStream imageInputStream = FilePart.getInputStream();
String FileName = FilePart.getSubmittedFileName();

int i = imageInputStream.available();
byte[]b = new byte[i];
imageInputStream.read(b);

FileOutputStream fos = new FileOutputStream("\\webapps\\Ass2\\images\\"+FileName);
fos.write(b);
imageInputStream.close();

Exception (System can't find the given path) enter image description here

I hope you guys can help me out.. I need to save the imagefile in the to the \webapp\Ass2\images\ folder.

Thank you very much.

Upvotes: 0

Views: 347

Answers (1)

SteveL
SteveL

Reputation: 3389

The error message is self explaining ,the path \webapp\Ass2\images\ doesnt exist,you need to create it first before putting a file in it.

String file="\\webapps\\Ass2\\images\\"+FileName;
File f = new File(file);
f.getParentFile().mkdirs(); 
f.createNewFile();

FileOutputStream fos = new FileOutputStream(file);
fos.write(b);
imageInputStream.close();

Upvotes: 2

Related Questions