java servlet如何获取提交的文件。
展开全部
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.dnion.contentpush.struts.BaseDispatchAction;
/**
* 提供web页面的提交的“文本”推送更新
* @author wxl
*
*/
public class WebTextPushAction extends BaseDispatchAction {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload fileUpload = new ServletFileUpload(factory);
//设置上传文件大小的上限,-1表示无上限
fileUpload.setSizeMax(-1);
List items = new ArrayList();
//上传文件,解析表单中包含的文件字段和普通字段
items = fileUpload.parseRequest(request);
//遍历字段进行处理
Iterator iterator = items.iterator();
while(iterator.hasNext()){
FileItem fileItem =(FileItem)iterator.next();
if(fileItem.isFormField()){//普通字段
}else{//文件字段
if("textfile".equals(fileItem.getFieldName())){
//上传;
}
}
}
return super.execute(mapping, form, request, response);
}
}
页面中form加上属性ENCTYPE="multipart/form-data"
<input name="textfile" type="file" value=“”/>
展开全部
这种问题 更适合去百度 Java servlet 文件上传 然后随便找个例子,照着跑起来,就好了,然后最好能去 了解下 Http 中 传输参数和传输文件的差异。传输文件 对应的ContentType ,以及现在流行起来的 文件存储系统,OSS,这些东西,文件上传可能造成的跨域问题。了解了这些就好了。其实,能从一般的参数,延伸到文件,其实挺好的。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
参考代码
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.dnion.contentpush.struts.BaseDispatchAction;
/**
* 提供web页面的提交的“文本”推送更新
* @author wxl
*
*/
public class WebTextPushAction extends BaseDispatchAction {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload fileUpload = new ServletFileUpload(factory);
//设置上传文件大小的上限,-1表示无上限
fileUpload.setSizeMax(-1);
List items = new ArrayList();
//上传文件,解析表单中包含的文件字段和普通字段
items = fileUpload.parseRequest(request);
//遍历字段进行处理
Iterator iterator = items.iterator();
while(iterator.hasNext()){
FileItem fileItem =(FileItem)iterator.next();
if(fileItem.isFormField()){//普通字段
}else{//文件字段
if("textfile".equals(fileItem.getFieldName())){
//上传;
}
}
}
return super.execute(mapping, form, request, response);
}
}
页面中form加上属性ENCTYPE="multipart/form-data"
<input name="textfile" type="file" value=“”/>
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.dnion.contentpush.struts.BaseDispatchAction;
/**
* 提供web页面的提交的“文本”推送更新
* @author wxl
*
*/
public class WebTextPushAction extends BaseDispatchAction {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload fileUpload = new ServletFileUpload(factory);
//设置上传文件大小的上限,-1表示无上限
fileUpload.setSizeMax(-1);
List items = new ArrayList();
//上传文件,解析表单中包含的文件字段和普通字段
items = fileUpload.parseRequest(request);
//遍历字段进行处理
Iterator iterator = items.iterator();
while(iterator.hasNext()){
FileItem fileItem =(FileItem)iterator.next();
if(fileItem.isFormField()){//普通字段
}else{//文件字段
if("textfile".equals(fileItem.getFieldName())){
//上传;
}
}
}
return super.execute(mapping, form, request, response);
}
}
页面中form加上属性ENCTYPE="multipart/form-data"
<input name="textfile" type="file" value=“”/>
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询