日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

jquery form java_springmvc利用jquery.form插件异步上传文件示例

發布時間:2025/3/17 c/c++ 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 jquery form java_springmvc利用jquery.form插件异步上传文件示例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

需要的下載文件:

jQuery.form.js

jquery.js

commons-fileupload.jar

commons-io.jar

示例圖片

pom.xml

commons-fileupload

commons-fileupload

1.3

commons-io

commons-io

2.4

web.xml 解決上傳后中文文件名亂碼問題

Set Character Encoding

org.springframework.web.filter.CharacterEncodingFilter

encoding

UTF-8

Set Character Encoding

/*

servlet-context.xml中添加對上傳的支持

jsp文件

String path = request.getContextPath();

String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path;

%>

springmvc上傳文件

springmvc上傳文件

項目名稱

File input

提交

$(function(){

$("#formSucc").hide();

$("#doSave").click(function(){

var requestUrl = "/widget/saveFile.json";

var projectName = $("#projectName").val();

$("#uploadForm").ajaxSubmit({

type: 'post',

url: requestUrl,

//data: {projectName: projectName}, //應該把這個去掉,要不然,值會有重復,因為form提交時已經做了一次提交了。

//如果projectName的值為"tt",如果這個地方不去掉,那么提交接收的值變成"tt,tt"了。

contentType: "application/x-www-form-urlencoded; charset=utf-8",

success: function(data) {

if(data.success){

$(".infoTips").remove();

$("#formSucc").show();

$("#formSucc").append(""+data.message+"");

}

}

});

});

});

Java的controller文件

package com.paincupid.springmvc.widget.controller;

import java.io.IOException;

import java.io.InputStream;

import java.util.List;

import javax.annotation.Resource;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.bind.annotation.ResponseBody;

import org.springframework.web.multipart.MultipartFile;

import com.paincupid.springmvc.finance.domain.Finance;

import com.paincupid.springmvc.test.domain.Person;

import com.paincupid.springmvc.util.BaseJsonRst;

import com.paincupid.springmvc.util.CreatMockData;

/**

*

* @author arthur.paincupid.lee

* @since 2016.01.24

*

*/

@Controller

@RequestMapping("/widget")

public class widgetController {

private static final Logger log = LoggerFactory.getLogger(widgetController.class);

/**

* 上傳文件

* 在前臺的訪問路徑為: http://localhost:8080/springmvc/widget/uploadFile/view

* @return

*/

@RequestMapping("/uploadFile/view")

public String uploadFile() {

return "widget/uploadFile";

}

@ResponseBody

@RequestMapping(value="/saveFile", method=RequestMethod.POST)

public BaseJsonRst saveFile(@RequestParam MultipartFile file,

@RequestParam String projectName) {

BaseJsonRst view = new BaseJsonRst();

String orgiginalFileName = "";

try {

String fileName = file.getName();

InputStream inputStream = file.getInputStream();

String content = file.getContentType();

orgiginalFileName = file.getOriginalFilename();

log.info("fileName: "+fileName+", inputStream: "+ inputStream

+"\r\n content: "+content+", orgiginalFileName: ="+ orgiginalFileName

+"\r\n projectName: "+ projectName);

} catch (IOException e) {

e.printStackTrace();

}

view.setSuccess(true);

view.setMessage("上傳: "+orgiginalFileName+" 文件成功!");

return view;

}

}

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

總結

以上是生活随笔為你收集整理的jquery form java_springmvc利用jquery.form插件异步上传文件示例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。