Artikel ini membagikan kode spesifik untuk unggahan file boot pegas untuk referensi Anda. Konten spesifiknya adalah sebagai berikut
1. Buat proyek web Maven, lalu konfigurasikan file pom.xml, dan tambahkan dependensi:
<dependency> <GroupId> org.springframework.boot </groupid> <ArTifactId> Spring-boot-starter-web </stifactid> <version> 1.0.2.release </version> </dependency>
2. Masukkan formulir di file index.jsp di direktori webapp:
<html> <body> <bentuk metode = "post" enctype = "multipart /form-data" action = " /unggah"> File untuk mengunggah: <input type = "file" name = "file"> <br /> nama: <input type = "name" name = "name"> <br /> <br /> <input type = "name" name = "name"> <br /> <br /> <input type = "submit" value "value" name "name" </form> </body>
Formulir ini adalah halaman Upload Simulasi kami
3. Tulis pengontrol yang memproses formulir ini:
impor java.io.bufferedoutputStream; impor java.io.file; impor java.io.fileoutputStream; impor org.springframework.stereotype.controller; impor org.springframework.web.bind.annotation.requestmapping; impor org.springframework.web.bind.annotation.requestmethod; impor org.springframework.web.bind.annotation.requestparam; impor org.springframework.web.bind.annotation.responseBody; impor org.springframework.web.multipart.multipartfile; @Controller kelas publik FileUploadController {@RequestMapping (value = "/unggah", Method = requestMethod.get) public @ResponseBody String menyediakan UPLoadInfo () {return "Anda dapat mengunggah file dengan memposting ke URL yang sama ini."; } @RequestMapping(value="/upload", method=RequestMethod.POST) public @ResponseBody String handleFileUpload(@RequestParam("name") String name, @RequestParam("file") MultipartFile file){ if (!file.isEmpty()) { try { byte[] bytes = file.getBytes(); Stream BufferedOutputStream = baru bufferedOutputStream (FileOutputStream baru (file baru (name + "-uploaded")))); stream.write (bytes); stream.close (); kembalikan "Anda berhasil diunggah" + nama + "ke" + nama + "-uploaded!"; } catch (Exception e) {return "Anda gagal mengunggah" + nama + "=>" + e.getMessage (); }} else {return "Anda gagal mengunggah" + nama + "karena file itu kosong."; }}}4. Lalu kami memiliki beberapa batasan pada file yang diunggah dan menulis metode utama untuk memulai web:
impor org.springframework.boot.springapplication; impor org.springframework.boot.autoconfigure.enableAutoconfiguration; impor org.springframework.boot.context.embedded.multipartconfigFactory; impor org.springframework.context.annotation.bean; impor org.springframework.context.annotation.componentscan; impor org.springframework.context.annotation.configuration; impor javax.servlet.multipartconfigelement; @Configuration @componentscan @EnableAutoconfiguration Aplikasi kelas publik {@bean Public MultipartConfigElement MultipartConfigElement () {MultipartConfigFactory factory = new MultipartConfigFactory (); factory.setmaxfileSize ("128KB"); factory.setmaxRequestSize ("128KB"); return factory.createMultipartConfig (); } public static void main (string [] args) {springApplication.run (application.class, args); }}5. Kemudian kunjungi http: // localhost: 8080/Unggah untuk melihat halaman.
Contoh di atas mengimplementasikan fungsi mengunggah satu file. Dengan asumsi bahwa kami ingin mengimplementasikan fungsi pengunggahan file batch sekarang, kami hanya perlu hanya memodifikasi kode di atas. Mempertimbangkan masalah panjang, berikut ini hanyalah kode yang berbeda dari yang di atas, dan deskripsi tidak diposting seperti di atas. :
1. Tambahkan file batchupload.jsp
<html> <body> <bentuk metode = "post" enctype = "multipart/form-data" action = "/batch/unggah"> file untuk mengunggah: <input type = "file" name = "file"> <br/> file untuk mengunggah: <input type = "file" name = "file"> <br/> <input type = "submit =" unggah "ke sini" </form> </body> </html>
2. Tambahkan file BatchFileUploadController.java:
impor org.springframework.steretype.controller; impor org.springframework.web.bind.annotation.requestmapping; impor org.springframework.web.bind.annotation.requestmethod; impor org.springframework.web.bind.annotation.responseBody; impor org.springframework.web.multipart.multipartfile; impor org.springframework.web.multipart.multipartttpservletRequest; impor javax.servlet.http.httpservletRequest; impor java.io.bufferedoutputStream; impor java.io.file; impor java.io.fileoutputStream; impor java.util.list; /*** Dibuat oleh Wenchao.Ren pada 2014/4/26. */@Controller kelas publik BatchFileUploadController {@RequestMapping (value = "/batch/unggah", metode = requestMethod.post) public @ResponseBody string handefileupload (httpservletRequest) {list <MultipartFile> File = (Multiparthtrps) {list. untuk (int i = 0; i <file.size (); ++ i) {multipartFile file = file.get (i); String name = file.getName (); if (! File.isempty ()) {coba {byte [] bytes = file.getbytes (); Stream BufferedOutputStream = baru bufferedOutputStream (FileOutputStream baru (file baru (nama + i))); stream.write (bytes); stream.close (); } catch (Exception e) {return "Anda gagal mengunggah" + nama + "=>" + e.getMessage (); }} else {return "Anda gagal mengunggah" + nama + "karena file itu kosong."; }} return "unggah sukses"; }}Fungsi unggahan batch sederhana seperti itu ok, bukankah itu sangat sederhana?
Catatan: Kode di atas hanya untuk demonstrasi, sehingga gaya pengkodean mengadopsi pendekatan kasual, dan tidak disarankan agar semua orang meniru itu.
Referensi: Multipartresolver mengimplementasikan fungsi unggahan file
Di atas adalah semua konten artikel ini. Saya berharap ini akan membantu untuk pembelajaran semua orang dan saya harap semua orang akan lebih mendukung wulin.com.
1. Multipartresolver juga dapat menerapkan fungsi unggahan file. Artikel referensi: