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();把文件上傳到指定的服務器上
jquery

 

方式二:
前端:沒有表單,用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();web

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();把文件上傳到指定的服務器上ajax


補充:
可以給服務端提交數據的方式
1.用form表單
2.用超連接
3.用ajax異步提交spring

相關文章
相關標籤/搜索