request.getSession().getServletContext().getRealPath("upload/")返回null是怎么回事?
<%@pageimport="java.io.File"%><%@pageimport="org.apache.commons.fileupload.disk.DiskF...
<%@page import="java.io.File"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="org.apache.commons.fileupload.FileItemFactory"%>
<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
request.setCharacterEncoding("utf-8");
String uploadFileName="";//上传的文件名
String fieldName="";//表单字段元素的name属性值
//请求信息中的内容是否是multipart类型
boolean isMultipart=ServletFileUpload.isMultipartContent(request);
//上传文件的存储路径(服务器文件系统上的绝对文件路径);
String uploadFilePath=request.getSession().getServletContext().getRealPath("upload/");
if(isMultipart){
FileItemFactory factory=new DiskFileItemFactory();
ServletFileUpload upload=new ServletFileUpload(factory);
//解析form表单中所有文件
List<FileItem> items=upload.parseRequest(request);
Iterator<FileItem> iterator=items.iterator();
while(iterator.hasNext()){//处理每个文件
FileItem item=(FileItem)iterator.next();
if(item.isFormField()){//普通表单字段
fieldName=item.getFieldName();//表单字段的name属性值
if(fieldName.equals("username")){
//输出表单字段的值
out.print(item.getString("utf-8")+"上传了文件。<br/>");
}else{//文件表单字段
String fileName=item.getName();
if(fieldName!=null&&!fileName.equals("")){
File fullFile=new File(item.getName());
File saveFile=new File(uploadFilePath,fullFile.getName());
item.write(saveFile);
uploadFileName=fullFile.getName();
out.print("上传后的文件名是:"+uploadFileName);
}
}
}
}
}
%>
表单提交页面 展开
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="org.apache.commons.fileupload.FileItemFactory"%>
<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
request.setCharacterEncoding("utf-8");
String uploadFileName="";//上传的文件名
String fieldName="";//表单字段元素的name属性值
//请求信息中的内容是否是multipart类型
boolean isMultipart=ServletFileUpload.isMultipartContent(request);
//上传文件的存储路径(服务器文件系统上的绝对文件路径);
String uploadFilePath=request.getSession().getServletContext().getRealPath("upload/");
if(isMultipart){
FileItemFactory factory=new DiskFileItemFactory();
ServletFileUpload upload=new ServletFileUpload(factory);
//解析form表单中所有文件
List<FileItem> items=upload.parseRequest(request);
Iterator<FileItem> iterator=items.iterator();
while(iterator.hasNext()){//处理每个文件
FileItem item=(FileItem)iterator.next();
if(item.isFormField()){//普通表单字段
fieldName=item.getFieldName();//表单字段的name属性值
if(fieldName.equals("username")){
//输出表单字段的值
out.print(item.getString("utf-8")+"上传了文件。<br/>");
}else{//文件表单字段
String fileName=item.getName();
if(fieldName!=null&&!fileName.equals("")){
File fullFile=new File(item.getName());
File saveFile=new File(uploadFilePath,fullFile.getName());
item.write(saveFile);
uploadFileName=fullFile.getName();
out.print("上传后的文件名是:"+uploadFileName);
}
}
}
}
}
%>
表单提交页面 展开
- 你的回答被采纳后将获得:
- 系统奖励15(财富值+成长值)+难题奖励20(财富值+成长值)
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询