希望对大家有帮助 不能直接用的 根据需求修改吧
spring 的配置文件需要设置:
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置上传文件的最大尺寸为1MB -->
<property name="maxUploadSize">
<value>10485760</value>
</property>
</bean>
------------必备JS
<script type="text/javascript" src="../js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="../js/ajaxfileupload.js"></script>
------------html
<form action="#" name="myform" method="post">
<input type='file' id="uploada" name="uploada" size='50'/>
<input type="button" style="width: 5em" onclick="uploadImages('uploada')" value='上传'/>
<input type='file' id="uploadb" name="uploadb" size='50'/>
<input type="button" style="width: 5em" onclick="uploadImages('uploadb')" value='上传'/>
<input type='file' id="uploadc" name="uploadc" size='50'/>
<input type="button" style="width: 5em" onclick="uploadImages('uploadc')" value='上传'/>
</form>
-----------AJAX
function uploadImages(type) {
$.ajaxFileUpload({
//跟具updateP得到不同的上传文本的ID
url:'${ctx}/activity/uploadImg.do?updateP='+type, //需要链接到服务器地址
secureuri:false,
fileElementId:''+type+'', //文件选择框的id属性(必须)
dataType: 'text',
success: function (data, status){
var data = eval("("+data+")");
if(data.updateP=='uploada'){
document.getElementById("显示文件访问路径文本的IDA").value=baseurl+"/upload/"+data.fileName;
}else if(data.updateP=='uploadb'){
document.getElementById("显示文件访问路径文本的IDB").value=baseurl+"/upload/"+data.fileName;
}else if(data.updateP=='uploadc'){
document.getElementById("显示文件访问路径文本的IDC").value=baseurl+"/upload/"+data.fileName;
}
alert('上传失败');
},
error: function (data, status, e){
alert('上传失败');
}
}
);
}
--------------spring
@RequestMapping("/activity/uploadImg.do")
public void uploadImg(MultipartHttpServletRequest request,MobileActivityJsp mobileActivityJsp,HttpServletResponse response) throws IOException{
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
SimpleDateFormat simpleFormat = new SimpleDateFormat("MMddHHmmsss");
String generationfileName = simpleFormat.format(new Date())+new Random().nextInt(1000);
//保存路径
String savePath = "D:/mobilePermissions/ddmap-mobile-medit/web/upload";
String updateP = request.getParameter("updateP");
String fileNameSuffix=null;
String fileName= null;
if(null!=updateP&&!"".equals(updateP)){
try {
MultipartFile mf = request.getFile(updateP);
fileName=mf.getOriginalFilename();
if(null!=mf&&!"".equals(mf)){
fileNameSuffix=fileName.substring(fileName.lastIndexOf(".")+1,fileName.length());
SaveFileFromInputStream(mf.getInputStream(),savePath,generationfileName+"."+fileNameSuffix);
out.write("{'state':'0','fileName':'"+generationfileName+"."+fileNameSuffix+"','updateP':'"+updateP+"'}");
}
} catch (Exception e) {
out.write("{'state':'1'}");
e.printStackTrace();
}
}
}
//保存文件 1,文件 2,保存路径 3,文件名称
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();
}