Javaweb文件上传的前端和后端

上传文件的分类:
无论什么方式上传文件,都要用post提交
方式一:
前端:表单方式上传文件
<form action="" method="post" enctype="multipart/form-data">
<!--非文件域-->
<input type="text" name="desc"/>
<!--文件域-->
<input type="file" name="userHead" />
<input type="submit" value="上传"/>
</form>

后端:
使用上传技术是apache中的Commons-fileupload.jar
commons-io.jar
servlet:
1.在表单提交的时候把表单中的所有的数据封装给request对象
2.通过commons-fileupload的api方法转换request对象
中的数据到一个List集合中
// Parse the request
List<FileItem> items = upload.parseRequest(request);
3.遍历 list集合,集合中都包含表单中所有的数据
包含文件域和非文件域
// Process the uploaded items
Iterator<FileItem> iter = items.iterator();
while (iter.hasNext()) {
FileItem item = iter.next();

if (item.isFormField()) {
//是非文件域
String name = item.getFieldName();
String value = item.getString();
...
} else {
//文件域
String fieldName = item.getFieldName();
String fileName = item.getName();
String contentType = item.getContentType();
boolean isInMemory = item.isInMemory();
long sizeInBytes = item.getSize();
...
//真正上传文件
item.write(服务端的某个目录)
}
}
spring mvc:
在springmvc中底层使用还是commons-fileupload.jar
和commons-io.jar,说明spring mvc对apache的Commons-fileupload
产品做二次封装,封装成:org.springframework.web.multipart.commons.CommonsMultipartResolver
在springmvc上传文件api用CommonsMultipartResolver类中的api
<!-- spring mvc 文件上传 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--能配置多少个property,可以查文档和查询源代码 -->
<!--最大上传文件的大小 -->
<property name="maxUploadSize" value="8388608"></property>
<property name="resolveLazily" value="true"></property>
</bean>

用springmvc的api上传文件
MultipartFile的对象调用一个上传方法
对象.transto();把文件上传到指定的服务器上

方式二:
前端:没有表单,用ajax上传文件,必须借助第三方
js工具ajaxfileupload.js,类似的上传文件
的js工具有很多,ajaxfileupload.js工具是基于
jquery库
//异步提交
$.ajaxFileUpload({
url:basePath+"user/new",//提交的服务器地址
secureuri:false,//url链接是否安全
fileElementId:"addHeadPicture",//文件域的id
type:"post",//必须是post提交
data:{"loginName":loginName,"password":password1,"nickName":nickName,"age":age,"sex":sex,"roleId":roleId},//传递的数据
dataType:"text",//注意text,可以写成json
success:function(data,status){
//alert(data);
//回的结果串中有其他的字符串,通过下面的方式
//把没用的字符串替换掉
data=data.replace(/<PRE.*?>/g,'');
data=data.replace("<PRE>",'');
data=data.replace("</PRE>",'');
data=data.replace(/<pre.*?>/g,'');
data=data.replace("<pre>",'');
data=data.replace("</pre>",'');
alert(data);
},
error:function(){
alert("请求失败!");
}
});



后端:
使用上传技术是apache中的Commons-fileupload.jar
commons-io.jar
servlet:
1.在表单提交的时候把表单中的所有的数据封装给request对象
2.通过commons-fileupload的api方法转换request对象
中的数据到一个List集合中
// Parse the request
List<FileItem> items = upload.parseRequest(request);
3.遍历 list集合,集合中都包含表单中所有的数据
包含文件域和非文件域
// Process the uploaded items
Iterator<FileItem> iter = items.iterator();
while (iter.hasNext()) {
FileItem item = iter.next();

if (item.isFormField()) {
//是非文件域
String name = item.getFieldName();
String value = item.getString();
...
} else {
//文件域
String fieldName = item.getFieldName();
String fileName = item.getName();
String contentType = item.getContentType();
boolean isInMemory = item.isInMemory();
long sizeInBytes = item.getSize();
...
//真正上传文件
item.write(服务端的某个目录)
}
}
spring mvc:
在springmvc中底层使用还是commons-fileupload.jar
和commons-io.jar,说明spring mvc对apache的Commons-fileupload
产品做二次封装,封装成:org.springframework.web.multipart.commons.CommonsMultipartResolver
在springmvc上传文件api用CommonsMultipartResolver类中的api
<!-- spring mvc 文件上传 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--能配置多少个property,可以查文档和查询源代码 -->
<!--最大上传文件的大小 -->
<property name="maxUploadSize" value="8388608"></property>
<property name="resolveLazily" value="true"></property>
</bean>

用springmvc的api上传文件
MultipartFile的对象调用一个上传方法
对象.transferTo();把文件上传到指定的服务器上


补充:
能够给服务端提交数据的方式
1.用form表单
2.用超链接
3.用ajax异步提交

猜你喜欢

转载自www.cnblogs.com/Edwin-future/p/9568174.html
今日推荐