I have a Javascript front end and a Java back end. For the front end HTML and Javascript I used a solution that someone else already provided here on Stack overflow.
HTML (provided):
<input type="file" name="file" onchange=angular.element(this).scope().uploadedFiles(this.files)"/>
JAVASCRIPT/Controller:
$scope.uploadFiles = function(files) {
var fd = new FormData();
//Take the first selected file
fd.append("file", files[0]);
var uploadUrl = GlobalService.resourceUrl('/equipment/imageupload');
$http.post(uploadUrl, fd, {
withCredentials: true,
headers: {'Content-Type': 'application/json' },
transformRequest: angular.identity
});
};
Backend method reached via REST (Java code)
@POST
@Path("/imageupload")
@Consumes(MediaType.APPLICATION_JSON)
public void uploadImage(InputStream uploadedStream)
{
try
{
File f = new File("C:\\Users\\myself\\myImage.jpg);
OutputStream os = new FileInputStream(f);
byte[] buf = new byte[1024];
int len;
while ((len = uploadedStream.read(buf)) > 0)
{
os.write(buf,0,len);
}
uploadedStream.close();
os.close();
}
catch (Exception e)
{
System.out.println("ERROR Uploading Image File);
}
System.out.println("Read input stream complete.");
}
When I select a file the post occurs and my Java backend code is reached correctly. The following is printed to the console:
-------WebKitFormBoundaryRANDOCHARACTERS
Content-Disposition: form-data; name = "file"; filename="mySourceImage.jpg"
Content-Type: image/jpeg
A LONG GROUP OF CHARACTERS THAT MUST BE THE IMAGE CONTENT
The uploaded file is larger than the source file. Uploaded server file = 53020 bytes in length, source client file = 52799 bytes in length. Does anybody have any idea what is causing this problem?
EDIT: Somebody asked for a printout of the two files, and what I'll do is post some of the results of the windows command fc /b. Problem is I do not have Internet access on the same machine as I develop on, and there is no network path between them. fc /b ORIGINALFILE UPLOADEDFILE This is the result for a PNG file I uploaded, which has the same type of problem: (LEFT = client original PNG file, RIGHT = uploaded PNG file)
00000000 89 2D
00000001 50 2D
00000002 4E 2D
00000003 47 2D
00000004 0D 2D
00000005 0A 2D
00000006 1A 57
00000007 0A 65
00000008 00 62
00000009 00 4B
Starting here you get the ?PNG that the client PNG file started with: (ONLY SERVER FILE BYTE COLUMN SHOWN HERE:)
00000086 89
00000087 50
00000088 4E
00000089 47
0000008A 0D
0000008B 0A
0000008C 1A
0000008D 0A
0000008E 00
0000008F 00
I hope that is somewhat illuminating. In the case of the png file, the Server file is 180 bytes longer than the client. Since there seems to be 134 characters of Junk at the front of the uploaded PNG file, it is not as simple as just truncating the first N bytes from the uploaded PNG file.