Can't upload attachment with right size with Java Plugin
Asked by Benjamin Kr?ger:

Hello,  
My idea:  
I try to send a zip to a java endpoint, where the zip is extracted and i read a json inside the zip to create a confluence page and add the zip as attachment.   
My code works till the attachment getting uploaded. I get the Error my inputstream is 0. How can i attach the complete zip? With FileUploadManager i can upload a File, but it is way to small. Where is the error in my code?

```
@Path("/software")
public class ConnectRest {

@ComponentImport private UserAccessor userAccessor;
@ComponentImport private PageManager pageManager;
@ComponentImport private SpaceManager spaceManager;
@ComponentImport private AttachmentManager attachmentManager;
@ComponentImport private FileUploadManager fileUploadManager;

public ConnectRest(UserAccessor userAccessor, PageManager pageManager, SpaceManager spaceManager, AttachmentManager attachmentManager, FileUploadManager fileUploadManager) {
this.userAccessor = userAccessor;
this.spaceManager = spaceManager;
this.pageManager = pageManager;
this.attachmentManager = attachmentManager;
this.fileUploadManager = fileUploadManager;
} 

@POST
@Produces({MediaType.APPLICATION_JSON})
@Consumes({MediaType.MULTIPART_FORM_DATA})
@Path("/postPackage")
public Response postPackage(
@Context SecurityContext sc,
@MultipartFormParam ("pageId") FilePart pageIdStr,
@MultipartFormParam ("file") FilePart file)
{
JSONObject response = new JSONObject();
try 
{
ConfluenceUser user = userAccessor.getUserByName(sc.getUserPrincipal().getName());
if(userAccessor.getGroupNames(user).contains("confluence-administrators"))
{
if(null != pageIdStr && null != file) {
int pageId = Integer.parseInt(pageIdStr.getValue());
if(file.getContentType().equals("application/octet-stream") && file.getName().endsWith(".package"))
{
InputStream inStream = file.getInputStream();
System.out.println(inStream.available() + "!!!!!!!!!!");
BufferedReader bin = new BufferedReader(new InputStreamReader(inStream));




Helper he = new Helper();
InputStream jsonInStream = he.getJSONofZip(inStream); //get Json in ZIP
if(null != jsonInStream)
{ 
Scanner s = new Scanner(jsonInStream).useDelimiter("\\A");
String result = s.hasNext() ? s.next() : "";
JSONObject json = new JSONObject(result);
String author = json.getString("author");
String version = json.getString("version");
String title = json.getString("title");
String description = json.getString("description");
String packagename = file.getName();
String template = "TemplatestringTooLong";
template = template.replaceAll(":author:", author);
template = template.replaceAll(":description:", description);
template = template.replaceAll(":version:", version);
template = template.replaceAll(":packagename:", packagename); 
Page targetPage = new Page();
Space space = spaceManager.getSpace("PACKAGES"); 
Page parentPage = space.getHomePage();
targetPage.setTitle(title);
targetPage.setSpace(space);
space.getHomePage().addChild(targetPage); 
targetPage.setParentPage(space.getHomePage());
targetPage.setCreatorName(user.getName());
targetPage.setBodyContent(new BodyContent(parentPage.getEntity(), template, parentPage.getDefaultBodyType()));
pageManager.saveContentEntity((ContentEntityObject)targetPage, (SaveContext)null);

// Try 1 to Upload File
Page page = pageManager.getPage("PACKAGES", title);
byte[] byteA = IOUtils.toByteArray(inStream); 
Attachment attachment = new Attachment(file.getName() , "application/zip", byteA.length, "", false);
attachment.setCreator(user);
attachment.setCreationDate(new Date());
attachment.setLastModificationDate(new Date());
attachment.setContainer((ContentEntityObject) page);
System.out.println(inStream.available() + "!!!!!!!!!!!!!!!!!!!");
attachmentManager.setAttachmentData(attachment, inStream);
attachmentManager.saveAttachment(attachment, null, inStream);
pageManager.saveContentEntity((ContentEntityObject)page, (SaveContext)null);


//Try 2 to Upload File
Page page = pageManager.getPage("PACKAGES", title);
byte[] byteA = IOUtils.toByteArray(bin);
AttachmentResource resource = new InputStreamAttachmentResource(new ByteArrayInputStream(byteA), packagename, "application/zip", byteA.length, "");
fileUploadManager.storeResource(resource, page);

}else {
return Response.status(422).entity(response.put("message", "No package.json in package file.").toString()).build();
}
}else{
return Response.status(422).entity(response.put("message", "Unrecognized file.").toString()).build();
}
}else {
return Response.status(422).entity(response.put("message", "It's necessary to post package file and pageId.").toString()).build();
}
}else{
return Response.status(401).entity(response.put("message", "You do not have the appropriate permissions.").toString()).build();
}}

catch (IOException ioe)
{return Response.status(422).entity(response.put("message", "Unrecognized file.").toString()).build();}
catch (NumberFormatException nfe)
{return Response.status(422).entity(response.put("message", "Unrecognized pageId.").toString()).build();}
catch (NullPointerException npe)
{return Response.status(422).entity(response.put("message", "It's necessary to post package file and pageId.").toString()).build();}}}



?
```


Answers:
author: Benjamin Kr?ger
body: So I found the problem. You can't upload a ByteArrayInputStream with the same InputStream i already read with my Helper class.  
New InputStream and the filesize was right.


Tags: attachment

confluence-development

confluence-server

java

server