十年網(wǎng)站開(kāi)發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶(hù) + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專(zhuān)業(yè)推廣+無(wú)憂(yōu)售后,網(wǎng)站問(wèn)題一站解決
使用MultipartFile怎么實(shí)現(xiàn)一個(gè)文件上傳功能?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。
創(chuàng)新互聯(lián)是一家以網(wǎng)絡(luò)技術(shù)公司,為中小企業(yè)提供網(wǎng)站維護(hù)、網(wǎng)站制作、成都網(wǎng)站制作、網(wǎng)站備案、服務(wù)器租用、國(guó)際域名空間、軟件開(kāi)發(fā)、小程序定制開(kāi)發(fā)等企業(yè)互聯(lián)網(wǎng)相關(guān)業(yè)務(wù),是一家有著豐富的互聯(lián)網(wǎng)運(yùn)營(yíng)推廣經(jīng)驗(yàn)的科技公司,有著多年的網(wǎng)站建站經(jīng)驗(yàn),致力于幫助中小企業(yè)在互聯(lián)網(wǎng)讓打出自已的品牌和口碑,讓企業(yè)在互聯(lián)網(wǎng)上打開(kāi)一個(gè)面向全國(guó)乃至全球的業(yè)務(wù)窗口:建站歡迎來(lái)電:028-86922220
一.主要有兩個(gè)java類(lèi),和一般的servlet放在一起即可.
1.FileUploadBean.java
package chb.demo.web; import org.springframework.web.multipart.MultipartFile; /** * @author chb * */ public class FileUploadBean { private MultipartFile file; public void setFile(MultipartFile file) { this.file = file; } public MultipartFile getFile() { return file; } }
2.FileUploadController.java
package chb.demo.web; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.validation.BindException; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.SimpleFormController; /** * @author chb * */ public class FileUploadController extends SimpleFormController { protected ModelAndView onSubmit( HttpServletRequest request, HttpServletResponse response, Object command, BindException errors){ try { // cast the bean FileUploadBean bean = (FileUploadBean) command; // let's see if there's content there MultipartFile file = bean.getFile(); if (file == null) { throw new Exception("上傳失?。何募?空"); } if(file.getSize()>10000000) { throw new Exception("上傳失敗:文件大小不能超過(guò)10M"); } //得到文件?名 String filename=file.getOriginalFilename(); if(file.getSize()>0){ try { SaveFileFromInputStream(file.getInputStream(),"D:/",filename); } catch (IOException e) { System.out.println(e.getMessage()); return null; } } else{ throw new Exception("上傳失?。荷蟼魑募荒転?空"); } // well, let's do nothing with the bean for now and return: try { return super.onSubmit(request, response, command, errors); } catch (Exception e) { System.out.println(e.getMessage()); return null; } } catch(Exception ex) { System.out.println(ex.getMessage()); return null; } } /**保存文件 * @param stream * @param path * @param filename * @throws IOException */ public void SaveFileFromInputStream(InputStream stream,String path,String filename) throws IOException { FileOutputStream fs=new FileOutputStream( path + "/"+ filename); byte[] buffer =new byte[1024*1024]; int bytesum = 0; int byteread = 0; while ((byteread=stream.read(buffer))!=-1) { bytesum+=byteread; fs.write(buffer,0,byteread); fs.flush(); } fs.close(); stream.close(); } }
二.配置文件中如下配置:
1.web.xml,利用spring mvc模式,大家應(yīng)該都很熟悉了
chb org.springframework.web.servlet.DispatcherServlet 1 chb *.do
2.chb-servlet.xml,這里要配置映射,并可以設(shè)定最大可上傳文件的大小
action index fileUploadController
三.設(shè)定jsp頁(yè)面
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。