激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - SpringMvc3+extjs4實現上傳與下載功能

SpringMvc3+extjs4實現上傳與下載功能

2020-11-25 10:52全力以赴001 Java教程

這篇文章主要為大家詳細介紹了SpringMvc3+extjs4實現上傳與下載功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了SpringMvc3+extjs4實現上傳下載的具體代碼,供大家參考,具體內容如下

最近生活過的很充實,人一直在不停的忙碌著學習新東西。這是我最近遇到的問題,我找度娘n了很久,終于找到了解決方案!

前臺代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<script>
 Ext.onReady(function() {
 
  Ext.create('Ext.form.Panel', {
   title : '文件上傳',
   width : 400,
   bodyPadding : 10,
   frame : true,
   renderTo : document.body,
   items : [ {
    xtype : 'filefield',
    name : '文件',
    fieldLabel : 'File',
    labelWidth : 50,
    msgTarget : 'side',
    allowBlank : false,
    anchor : '100%',
    buttonText : '請選擇文件...'
   } ],
 
   buttons : [ {
    text : '上傳',
    handler : function() {
     var form = this.up('form').getForm();
     if (form.isValid()) {
      form.submit({
       url : '根路徑/fileUploadDown/fileUpload',
       waitMsg : '正在上傳文件中...',
       success : function(fp, o) {
        Ext.Msg.alert('上傳文件成功!');
       }
      });
     }
    }
   } ]
  });
 
 });
</script>

后臺代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
*記錄返回結果*/
 class ExtJSFormResult {
 
 private boolean success;
 
 public boolean isSuccess() {
  return success;
 }
 
 public void setSuccess(boolean success) {
 
 }
 
 public String toString() {
  return "{success:" + this.success + "}";
 }
}
 
 
class FileUploadBean {
 
  private CommonsMultipartFile file;
 
  public CommonsMultipartFile getFile() {
   return file;
  }
 
  public void setFile(CommonsMultipartFile file) {
   this.file = file;
  }
}
 
/**
 * 文件的上傳與下載
 * @author Administrator
 *
 */
@Controller
@RequestMapping(value = "/fileUploadDown")
public class FileUploadAndDownController {
 
 private static int countter=1; //定義一個計數器,用于上傳文件的重命名
 
 @Autowired
 private ProAnnexDao<ProAnnex> proAnnextDao;
 
 
 
 public void setProAnnextDao(ProAnnexDao<ProAnnex> proAnnextDao) {
  this.proAnnextDao = proAnnextDao;
 }
 
 @RequestMapping(value="fileUpload",method = RequestMethod.POST)
 public @ResponseBody String create(RedirectAttributes redirectAttributes,FileUploadBean uploadItem,
   BindingResult result,HttpSession session){
  //獲取根路徑
  String uploadFolderPath = session.getServletContext().getRealPath("/");
  ExtJSFormResult extjsFormResult = new ExtJSFormResult();
  try {
   
   if (result.hasErrors()) {
    for (ObjectError error : result.getAllErrors()) {
     System.err.println("Error: " + error.getCode() + " - "
       + error.getDefaultMessage());
    }
 
    // 設置ExtJS返回 - error
    extjsFormResult.setSuccess(false);
 
    return extjsFormResult.toString();
   }
 
   MultipartFile file = uploadItem.getFile();
   String fileName = null;
   InputStream inputStream = null;
   OutputStream outputStream = null;
   if(file.getSize()>0){
     System.out.println("File Size:::" + file.getSize());
    if(file.getSize()>5242880){
      System.out.println("File Size:::" + file.getSize());
      extjsFormResult.setSuccess(false);
     return "error";
    }
    
    inputStream = file.getInputStream();
  
    File newFile = new File(uploadFolderPath + "fileUpload/");
    //如果文件路徑不存在就新建一個
    if(!newFile.exists()){
     newFile.mkdirs();
    }
    //獲取文件名
    String name=file.getOriginalFilename();
    //從數據庫中查詢存在此類文件名否
    Long count=proAnnextDao.isRepeatName(name);
    //如果存在一樣的文件名,就進行從命名
    if (count>0) {
     name=name.substring(0, name.lastIndexOf("."))+"("+(countter++)+")"+name.substring(name.lastIndexOf("."));
    }
    
    fileName = uploadFolderPath + "fileUpload/" + name;
    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.close();
    inputStream.close();
    
    
   }
 
   // 設置ExtJS返回 - sucsess
   extjsFormResult.setSuccess(true);
  } catch (Exception e) {
   
   e.printStackTrace();
   // 設置ExtJS返回 - error
  
   extjsFormResult.setSuccess(false);
  }
  
 
  return extjsFormResult.toString();
 }
 
 
}

springMvc.xml(此文件名可能跟項目的實際情況有區別)中的配置:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
<!-- 上傳文件,限制大小的配置 -->
  <bean id="multipartResolver"
  class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
   <!--resolveLazily屬性啟用是為了推遲文件解析,以便在Upload中捕獲文件大小異常-->
  <property name="resolveLazily" value="true"/>
  <property name="maxUploadSize" value="5242880" />
 </bean>
 
 
 <!-- 將無法mapping到Controller的path交給default servlet handler處理  -->
 <mvc:default-servlet-handler/><!-- 使用默認的servlet來響應靜態文件 -->
 <!-- 文件的上傳與下載 -->
 <mvc:view-controller path="/" view-name="redirect:/fileUploadDown"/>

以上的就是上傳文件了。 

下載呢?

下載比較簡單,代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
@RequestMapping("/downloadFile")
 public void download(@Valid @ModelAttribute("downLoadName") String downLoadName,
   HttpServletResponse response,HttpSession session,BindingResult result,HttpServletRequest request) throws IOException {
  
  response.setCharacterEncoding("UTF-8");
  request.setCharacterEncoding("UTF-8");
  //獲取文件的路徑
  String url=session.getServletContext().getRealPath("/")+"/fileUpload/"+downLoadName;
  System.out.println(url);
  File file=new File(url);
  
  InputStream input = FileUtils.openInputStream(file);
  byte[] data = IOUtils.toByteArray(input);
 
  //System.out.println("文件名:"+downLoadName);
  response.reset();
  //設置響應的報頭信息(中文問題解決辦法)
  response.setHeader("content-disposition","attachment;fileName="+URLEncoder.encode(downLoadName, "UTF-8"));
  response.addHeader("Content-Length", "" + data.length);
  response.setContentType("application/octet-stream; charset=UTF-8");
  
  IOUtils.write(data, response.getOutputStream());
  IOUtils.closeQuietly(input);
  
 }

在界面上只要有一個連接地址:如:window.location.href="根路徑/fileUploadDown/downfile/downLoadName="+name;這樣就可以下載了....   超連接的寫法基本一樣,這里就不多說了.

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产交换3p国产精品 | 亚洲欧美在线视频免费 | 日韩视频1| 视频一区二区三区在线播放 | 久久综合狠狠综合久久 | 精品国产一级毛片 | 免费国产在线视频 | 九九热在线精品视频 | www国产成人免费观看视频,深夜成人网 | 草草影院地址 | 久久久成人一区二区免费影院 | 史上最强炼体老祖动漫在线观看 | 欧美日韩国产成人在线 | 欧美另类视频在线 | av在线收看 | 久草在线最新 | 四虎影院成人 | 亚洲一区二区三区在线 | 性欧美一区 | 欧美黑人xx | 国产乱淫av片免费观看 | 曰批全过程120分钟免费69 | 粉嫩粉嫩一区二区三区在线播放 | 一级做受大片免费视频 | 国产女厕所 | 免费看成人毛片 | 视频一区二区不卡 | 精品中文字幕久久久久四十五十骆 | 国产精品视频久久久 | 国产精品久久久久免费视频 | 成人做爽爽爽爽免费国产软件 | 56av国产精品久久久久久久 | 在线看毛片的网站 | 欧美精品久久久久久久久久 | 极色品影院 | 在线小视频国产 | 精品国产91久久久久久久妲己 | 国产成年人视频 | 久久九九热re6这里有精品 | 亚洲国产成人久久成人52 | 国产免费一区二区三区 |