您好,登錄后才能下訂單哦!
前臺(tái)使用的是jquery的jquery.uploadify-v2.1.0插件,使用ajax。使用代碼如下:
- XML/HTML代碼
- jQuery("#uploadify").uploadify({
- 'script' : '../p_w_picpathUpload/upload/' + $("#sortId").val() + '.json',
這句話就是將請(qǐng)求發(fā)送到對(duì)應(yīng)的uploadcontroller中,sortId是表示文件所屬分類。
看下SpringMVC的使用方法:
在配置文件中添加如下配置:該文件即是SpringMVC對(duì)應(yīng)的DispatcherServlet配置文件
- XML/HTML代碼
- <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
- <property name="defaultEncoding" value="utf-8"></property>
- </bean>
一開始使用如下的方法上傳:
- Java代碼
- @RequestMapping(method = RequestMethod.POST, params = "action=upload")
- public String upload(ModelMap model, MultipartFile uploadify,BindingResult result) {
- }
結(jié)果報(bào)錯(cuò)了:
org.springframework.beans.BeanInstantiationException: Could not instantiate bean class
[org.springframework.web.multipart.MultipartFile]: Specified class is an interface
無法實(shí)例化,不知道是什么原因,對(duì)Spring不太熟悉,只能嘗試用其他方式來實(shí)現(xiàn)了。
后來改成下面這種實(shí)現(xiàn)了:
- Java代碼
- @RequestMapping(value = "/upload" + SORTID_BINDER_PATH, method = RequestMethod.POST)
- public void upload(HttpServletRequest request, HttpServletResponse response, @PathVariable java.lang.Integer sortId) {
- CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
- commonsMultipartResolver.setDefaultEncoding("utf-8");
- if (commonsMultipartResolver.isMultipart(request)) {
- MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
- Iterator<String> iter = multipartRequest.getFileNames();
- while (iter.hasNext()) {
- MultipartFile file = multipartRequest.getFile((String) iter.next());
- if (file != null) {
- String fileName = "";
- fileName = sdf.format(new Date()) + "_" + file.getOriginalFilename();
- String path = filePath + fileName;
- uploadPath += fileName;
- //重點(diǎn)就是這兩句
- File localFile = new File(path);
- file.transferTo(localFile);
- }
- }
- }
希望大家有好的方法提出來,一起學(xué)習(xí)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。