Upload and download java files

File upload and file download, this should be the most basic thing, every programmer should know. However, it really wasn't before, hahahaha

I won't find a wife! ! ! After looking for Du Niang and writing the code, wow, it's so simple. ------------------------------ I encountered this problem again next time, how did mmp write it?

Well, the above is my rant. To prevent this problem from recurring, the upload and download of the file should be processed once.

Brain-damaged + young monkey recording method :

File upload and download: first come to a file Here is a file class File 

Yes, you now have a file class. Then, to instantiate a file, you need to specify the file path.

File file = new File(path); path is the location where the file is stored in the system, well, what is the relative path, the absolute path is still unclear. . . Don't care, if there is a problem, let's go to Du Niang.

Now we need to clarify whether we are uploading or downloading;

document dowload:

Downloading is the easiest. I have never understood download,,,,, file download before. First of all, we need to know what file to download and how to determine the path of this file. Yes, pass the path of this file to the front end, he knows how to download it, and specifically to understand, what Windows.open(), a tag, open the url, and the browser will do the download. . One thing to note here is that the file path you give should be on the public Internet.

File Upload:

FIle file = new File(path);

File fileNew = MultipartFile file;//Under the image representation, if it is not true, fileNew is the file uploaded by the user

fileNew.transferTo(file); this uploads the file to the location you specified

Keep it simple. To tell you the truth, I am writing while reading the code.

If you want to read the content of the file, this has to be distinguished. The way of reading the format of different files is different.

For example, excel and txt have different reading methods.

file read and write

fileinputstream, fileOutPutStream stream operations on files, I think it is to put a file in a

bufferReader, bufferwriter read and write files

Close open resources




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325561879&siteId=291194637