Can anyone please explain how the Spring's Multipartfile works?












1















i've been trying to understand how the multipartfile works in spring
but did't find out any helpful info on internet.




Trying to understand this method:



 public String copyUploadedImage(MultipartFile multipartFile, String realpath, int userId) throws IOException {
String orgFileName = null;
orgFileName = multipartFile.getOriginalFilename();
String fileName = null;
InputStream inputStream = null;
OutputStream outputStream = null;
if (multipartFile.getSize() > 0) {
inputStream = multipartFile.getInputStream();
String root = realpath + File.separator + userId + File.separator + "Image" + File.separator;
File file = new File(root + File.separator);
if (file.mkdirs()) {
System.out.println("Directory is created!");
} else {
System.out.println("Failed to create directory!");
}
fileName = file + File.separator + multipartFile.getOriginalFilename();
outputStream = new FileOutputStream(fileName);
int readBytes = 0;
byte buffer = new byte[10000];
while ((readBytes = inputStream.read(buffer, 0, 10000)) != -1) {

outputStream.write(buffer, 0, readBytes);
}
outputStream.flush();
outputStream.close();
inputStream.close();
}
return orgFileName;
}









share|improve this question






















  • What exactly do you want to know? How MultipartFile works or what the method is doing (short answer: it saves an uploaded (image) file in a directory based on the given realpath and userId parameters)?
    – quatax
    Nov 20 at 5:50












  • @quatax thanks for response. I know why it is used in applications but i just want to know how it works internally to store a file into DB.
    – SHAIK BAJI VALI
    Nov 20 at 6:24










  • The file isn't stored in a database in this method. It gets written to the file system.
    – quatax
    Nov 20 at 6:28










  • @quatax ,ok but how it actually works internally?
    – SHAIK BAJI VALI
    Nov 20 at 6:32










  • So, you want to know how Java writes Bytes to the file system?
    – quatax
    Nov 20 at 6:42
















1















i've been trying to understand how the multipartfile works in spring
but did't find out any helpful info on internet.




Trying to understand this method:



 public String copyUploadedImage(MultipartFile multipartFile, String realpath, int userId) throws IOException {
String orgFileName = null;
orgFileName = multipartFile.getOriginalFilename();
String fileName = null;
InputStream inputStream = null;
OutputStream outputStream = null;
if (multipartFile.getSize() > 0) {
inputStream = multipartFile.getInputStream();
String root = realpath + File.separator + userId + File.separator + "Image" + File.separator;
File file = new File(root + File.separator);
if (file.mkdirs()) {
System.out.println("Directory is created!");
} else {
System.out.println("Failed to create directory!");
}
fileName = file + File.separator + multipartFile.getOriginalFilename();
outputStream = new FileOutputStream(fileName);
int readBytes = 0;
byte buffer = new byte[10000];
while ((readBytes = inputStream.read(buffer, 0, 10000)) != -1) {

outputStream.write(buffer, 0, readBytes);
}
outputStream.flush();
outputStream.close();
inputStream.close();
}
return orgFileName;
}









share|improve this question






















  • What exactly do you want to know? How MultipartFile works or what the method is doing (short answer: it saves an uploaded (image) file in a directory based on the given realpath and userId parameters)?
    – quatax
    Nov 20 at 5:50












  • @quatax thanks for response. I know why it is used in applications but i just want to know how it works internally to store a file into DB.
    – SHAIK BAJI VALI
    Nov 20 at 6:24










  • The file isn't stored in a database in this method. It gets written to the file system.
    – quatax
    Nov 20 at 6:28










  • @quatax ,ok but how it actually works internally?
    – SHAIK BAJI VALI
    Nov 20 at 6:32










  • So, you want to know how Java writes Bytes to the file system?
    – quatax
    Nov 20 at 6:42














1












1








1








i've been trying to understand how the multipartfile works in spring
but did't find out any helpful info on internet.




Trying to understand this method:



 public String copyUploadedImage(MultipartFile multipartFile, String realpath, int userId) throws IOException {
String orgFileName = null;
orgFileName = multipartFile.getOriginalFilename();
String fileName = null;
InputStream inputStream = null;
OutputStream outputStream = null;
if (multipartFile.getSize() > 0) {
inputStream = multipartFile.getInputStream();
String root = realpath + File.separator + userId + File.separator + "Image" + File.separator;
File file = new File(root + File.separator);
if (file.mkdirs()) {
System.out.println("Directory is created!");
} else {
System.out.println("Failed to create directory!");
}
fileName = file + File.separator + multipartFile.getOriginalFilename();
outputStream = new FileOutputStream(fileName);
int readBytes = 0;
byte buffer = new byte[10000];
while ((readBytes = inputStream.read(buffer, 0, 10000)) != -1) {

outputStream.write(buffer, 0, readBytes);
}
outputStream.flush();
outputStream.close();
inputStream.close();
}
return orgFileName;
}









share|improve this question














i've been trying to understand how the multipartfile works in spring
but did't find out any helpful info on internet.




Trying to understand this method:



 public String copyUploadedImage(MultipartFile multipartFile, String realpath, int userId) throws IOException {
String orgFileName = null;
orgFileName = multipartFile.getOriginalFilename();
String fileName = null;
InputStream inputStream = null;
OutputStream outputStream = null;
if (multipartFile.getSize() > 0) {
inputStream = multipartFile.getInputStream();
String root = realpath + File.separator + userId + File.separator + "Image" + File.separator;
File file = new File(root + File.separator);
if (file.mkdirs()) {
System.out.println("Directory is created!");
} else {
System.out.println("Failed to create directory!");
}
fileName = file + File.separator + multipartFile.getOriginalFilename();
outputStream = new FileOutputStream(fileName);
int readBytes = 0;
byte buffer = new byte[10000];
while ((readBytes = inputStream.read(buffer, 0, 10000)) != -1) {

outputStream.write(buffer, 0, readBytes);
}
outputStream.flush();
outputStream.close();
inputStream.close();
}
return orgFileName;
}






java spring-mvc






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 at 3:58









SHAIK BAJI VALI

316




316












  • What exactly do you want to know? How MultipartFile works or what the method is doing (short answer: it saves an uploaded (image) file in a directory based on the given realpath and userId parameters)?
    – quatax
    Nov 20 at 5:50












  • @quatax thanks for response. I know why it is used in applications but i just want to know how it works internally to store a file into DB.
    – SHAIK BAJI VALI
    Nov 20 at 6:24










  • The file isn't stored in a database in this method. It gets written to the file system.
    – quatax
    Nov 20 at 6:28










  • @quatax ,ok but how it actually works internally?
    – SHAIK BAJI VALI
    Nov 20 at 6:32










  • So, you want to know how Java writes Bytes to the file system?
    – quatax
    Nov 20 at 6:42


















  • What exactly do you want to know? How MultipartFile works or what the method is doing (short answer: it saves an uploaded (image) file in a directory based on the given realpath and userId parameters)?
    – quatax
    Nov 20 at 5:50












  • @quatax thanks for response. I know why it is used in applications but i just want to know how it works internally to store a file into DB.
    – SHAIK BAJI VALI
    Nov 20 at 6:24










  • The file isn't stored in a database in this method. It gets written to the file system.
    – quatax
    Nov 20 at 6:28










  • @quatax ,ok but how it actually works internally?
    – SHAIK BAJI VALI
    Nov 20 at 6:32










  • So, you want to know how Java writes Bytes to the file system?
    – quatax
    Nov 20 at 6:42
















What exactly do you want to know? How MultipartFile works or what the method is doing (short answer: it saves an uploaded (image) file in a directory based on the given realpath and userId parameters)?
– quatax
Nov 20 at 5:50






What exactly do you want to know? How MultipartFile works or what the method is doing (short answer: it saves an uploaded (image) file in a directory based on the given realpath and userId parameters)?
– quatax
Nov 20 at 5:50














@quatax thanks for response. I know why it is used in applications but i just want to know how it works internally to store a file into DB.
– SHAIK BAJI VALI
Nov 20 at 6:24




@quatax thanks for response. I know why it is used in applications but i just want to know how it works internally to store a file into DB.
– SHAIK BAJI VALI
Nov 20 at 6:24












The file isn't stored in a database in this method. It gets written to the file system.
– quatax
Nov 20 at 6:28




The file isn't stored in a database in this method. It gets written to the file system.
– quatax
Nov 20 at 6:28












@quatax ,ok but how it actually works internally?
– SHAIK BAJI VALI
Nov 20 at 6:32




@quatax ,ok but how it actually works internally?
– SHAIK BAJI VALI
Nov 20 at 6:32












So, you want to know how Java writes Bytes to the file system?
– quatax
Nov 20 at 6:42




So, you want to know how Java writes Bytes to the file system?
– quatax
Nov 20 at 6:42

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53386007%2fcan-anyone-please-explain-how-the-springs-multipartfile-works%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53386007%2fcan-anyone-please-explain-how-the-springs-multipartfile-works%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

RAC Tourist Trophy