Well, the basic problem with your code is that you read the entire uploaded file into memory (that byte array at line 48). And then you copy that into a
String (at line 55), which takes up twice as much memory as that byte array (one char == two bytes). So a big enough upload file will cause your application to run out of memory and crash.
So you have to rewrite so that you don't have the whole upload in memory all at once. I can't tell you exactly how to do that, because I can't exactly tell what the code starting at line 59 is trying to do. But somehow you have to start reading the data and doing that logic as you read the first few lines. It looks like the code is supposed to only write out a subset of the upload, and that subset is specified by header data in the upload? So once you have extracted the information from the header you would carry on reading, and only write the specified data to the output file.
The important part of that is that
you should only read a small amount of data at any one time.