一、將文件上傳到FileLoadServlet處理web
- Ext.onReady(function() {
- var form = new Ext.form.FormPanel({
- labelAlign : 'right',
- title : 'form',
- labelWidth : 50,
- buttonAlign : 'center',
- frame : true,
- fileUpload : true,
- url : 'FileLoadServlet',
- width : 280,
- items : [ {
- xtype : 'textfield',
- fieldLabel : '文本框',
- name : 'file',
- inputType : 'file'
- } ],
- buttons : [ {
- text : '提交',
- handler : function() {
- form.getForm().submit({
- success : function(form, action) {
- Ext.Msg.alert('信息', action.result.msg);
- },
- failure : function() {
- Ext.Msg.alert('錯誤', '失敗');
- }
- });
- }
- } ]
- });
- form.render("form");
- });
二、servlet類處理app
(1)web.xmlide
- <servlet>
- <servlet-name>FileLoadServlet</servlet-name>
- <servlet-class>com.alibaba.upload.FileLoadServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>FileLoadServlet</servlet-name>
- <url-pattern>/FileLoadServlet</url-pattern>
- </servlet-mapping>
(2)具體的類處理url
- FileItemFactory factory = new DiskFileItemFactory();
- ServletFileUpload fileload = new ServletFileUpload(factory);
- fileload.setSizeMax(4194304);
- try {
- List<FileItem> fileItems = (List<FileItem>) fileload.parseRequest(request);
- Iterator<FileItem> iter = fileItems.iterator(); // 依次處理每一個上傳的文件
- while (iter.hasNext()) {
- FileItem item = (FileItem) iter.next();// 忽略其餘不是文件域的全部表單信息
- if (!item.isFormField()) {
- String name = item.getName();//獲取上傳文件名,包括路徑
- System.out.println(name);
- if (name != null) {
- File filetoserver = new File("D:\\", name);
- item.write(filetoserver);
- System.out.println("文件:" + name + "上傳成功,大小是" + item.getSize());
- }
- }
- }
- } catch (Exception e) {
- }
- response.getWriter().print("{success:true,msg:'上傳成功'}");
【注意】spa
一、須要加入fileupload包,因爲其使用的common-io包,同時也須要加入其中。orm