request.setCharacterEncoding("utf-8");
//这里对request进行封装,RequestContext提供了对request多个访问方法
RequestContext requestContext = new ServletRequestContext(request);
//判断表单是否是Multipart类型的。这里可以直接对request进行判断,不过已经以前的用法了
if (FileUpload.isMultipartContent(requestContext)) {
DiskFileItemFactory factory = new DiskFileItemFactory();
// 设置文件的缓存路径
File d = new File("D:/temp");
if(!d.exists()){
d.mkdirs();
}
factory.setRepository(d);
ServletFileUpload upload = new ServletFileUpload(factory);
//设置上传文件大小的上限,-1表示无上限
upload.setSizeMax(-1);
List items = new ArrayList();
try {
//上传文件,并解析出所有的表单字段,包括普通字段和文件字段
items = upload.parseRequest(request);
} catch (FileUploadException e1) {
System.out.println("上传发生异常" + e1.getMessage());
}
//下面对每个字段进行处理,分普通字段和文件字段
Iterator it = items.iterator();
while (it.hasNext()) {
FileItem fileItem = (FileItem) it.next();
//如果是普通字段
if (fileItem.isFormField()) {
System.out.println("文本框的name属性:"
+ fileItem.getFieldName()
+ "\n文件名:"
+ fileItem.getName()
+ "\n上传的文件路径:"
+ new String(fileItem.getString().getBytes(
"iso8859-1"), "utf-8"));
} else {
System.out.println("文本框的name属性:"
+ fileItem.getFieldName() + "\n文件名:"
+ fileItem.getName() + "\n是否在内存中:"
+ fileItem.isInMemory() + "\n文件类型:"
+ fileItem.getContentType() + "\n文件大小:"
+ fileItem.getSize());
//保存文件,其实就是把缓存里的数据写到目标路径下
if (fileItem.getName() != null && fileItem.getSize() != 0) {
File fullFile = new File(fileItem.getName());
File newFile = new File("D:\\upload"+ fullFile.getName());//想换成webroot下的upload路径。。。。。。。。。。。
try {
fileItem.write(newFile);
} catch (Exception e) {
e.printStackTrace();
}
} else {
System.out.println("文件没有选择 或 文件内容为空");
}
}
System.out.println("=============================================");
}
}
13 个解决方案
#1
3.在Servlet中取得路径:
(1)得到工程目录:request.getSession().getServletContext().getRealPath("") 参数可具体到包名。
结果:E:\Tomcat\webapps\TEST
(2)得到IE地址栏地址:request.getRequestURL()
结果:http://localhost:8080/TEST/test
(3)得到相对地址:request.getRequestURI()
结果:/TEST/test
http://hi.baidu.com/java1016/blog/item/dfe50807964eac7c030881b6.html
(1)得到工程目录:request.getSession().getServletContext().getRealPath("") 参数可具体到包名。
结果:E:\Tomcat\webapps\TEST
(2)得到IE地址栏地址:request.getRequestURL()
结果:http://localhost:8080/TEST/test
(3)得到相对地址:request.getRequestURI()
结果:/TEST/test
http://hi.baidu.com/java1016/blog/item/dfe50807964eac7c030881b6.html
#2
学习
#3
request.getSession().getServletContext().getRealPath("/upload") 我用了这个 但是上传不到啊,也不报错。哪位高手指点啊
#4
request.getSession().getServletContext().getRealPath("/WEB-INF/upload")
#5
都不行啊,哪位大哥救命啊!!!
老师这样写的可以。 openStream ?为什么呢?怎么让传上去的文件名和之前一样的?
package com.cstp.upload;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
/**
* Servlet implementation class for Servlet: FileUpload1
*
*/
public class FileUpload1 extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
static final long serialVersionUID = 1L;
private String path;
public FileUpload1() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if(ServletFileUpload.isMultipartContent(request)){
FileItemFactory dfif=new DiskFileItemFactory();
System.out.println("=================================================");
PrintWriter w = response.getWriter();
ServletFileUpload dfu=new ServletFileUpload(dfif);
dfu.setHeaderEncoding("utf-8");
try {
FileItemIterator item = dfu.getItemIterator(request);
while(item.hasNext()){
FileItemStream obj = item.next();
System.out.print("是否为普通文本框:"+obj.isFormField()+"\t");
System.out.print("\n文本框的name属性:"+obj.getFieldName()+"\t");
System.out.println("\n文件类型:"+obj.getContentType());
if(!obj.isFormField()){
InputStream in = obj.openStream();
System.out.println("\n被上传文件路径:"+obj.getName());
if(obj.getName().trim().length()<1)
continue;
//File file=new File(path);
File file = File.createTempFile("temp_",obj.getName().substring(obj.getName().lastIndexOf('.')),new File(path));
OutputStream out = new FileOutputStream(file);
int i;
while((i=in.read())!=-1)
out.write(i);
System.out.println("\n存放文件的路径:"+file.getAbsolutePath());
out.close();
w.println("<img src='./upload/"+file.getName()+"'>");
}
System.out.println("-------------------------------------------------------------------------------");
}
} catch (FileUploadException e) {
e.printStackTrace();
}
}
}
@Override
public void init() throws ServletException {
super.init();
System.out.println(super.getServletContext().getRealPath("/upload"));
path=super.getInitParameter("path");
}
}
老师这样写的可以。 openStream ?为什么呢?怎么让传上去的文件名和之前一样的?
package com.cstp.upload;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
/**
* Servlet implementation class for Servlet: FileUpload1
*
*/
public class FileUpload1 extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
static final long serialVersionUID = 1L;
private String path;
public FileUpload1() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if(ServletFileUpload.isMultipartContent(request)){
FileItemFactory dfif=new DiskFileItemFactory();
System.out.println("=================================================");
PrintWriter w = response.getWriter();
ServletFileUpload dfu=new ServletFileUpload(dfif);
dfu.setHeaderEncoding("utf-8");
try {
FileItemIterator item = dfu.getItemIterator(request);
while(item.hasNext()){
FileItemStream obj = item.next();
System.out.print("是否为普通文本框:"+obj.isFormField()+"\t");
System.out.print("\n文本框的name属性:"+obj.getFieldName()+"\t");
System.out.println("\n文件类型:"+obj.getContentType());
if(!obj.isFormField()){
InputStream in = obj.openStream();
System.out.println("\n被上传文件路径:"+obj.getName());
if(obj.getName().trim().length()<1)
continue;
//File file=new File(path);
File file = File.createTempFile("temp_",obj.getName().substring(obj.getName().lastIndexOf('.')),new File(path));
OutputStream out = new FileOutputStream(file);
int i;
while((i=in.read())!=-1)
out.write(i);
System.out.println("\n存放文件的路径:"+file.getAbsolutePath());
out.close();
w.println("<img src='./upload/"+file.getName()+"'>");
}
System.out.println("-------------------------------------------------------------------------------");
}
} catch (FileUploadException e) {
e.printStackTrace();
}
}
}
@Override
public void init() throws ServletException {
super.init();
System.out.println(super.getServletContext().getRealPath("/upload"));
path=super.getInitParameter("path");
}
}
#6
是不是你的网站在war里,如果是这样的话 request.getSession().getServletContext().getRealPath 会返回 null
#7
我遇到的也是这样的情况 至今还没有解决
#8
可不可以直接部署网站,而不是不网站放到war包里。这样不就行了。
#9
到网上找一下,smartupload,fileupload的资料吧!
#10
问题解决了,实际上是2个环境的问题。一个开发环境,一个是实际环境。
上传后 webroot下的upload文件夹里看不到,但是去上传的绝对路径也就是程序中的path:D:\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\FileUpload\upload 可以看到上传成功了,但是找到项目的路径D:\workspace\FileUpload\WebContent\upload是没有的,所以在项目里怎么刷新也看不到。老师说是一个是开发路径,一个是实际路径。直接用,是可以得到的,只是在项目里看不到。我看的视频,那个风中叶老师的struts2视频的第13讲,可以看到,可能是MyEclipse的视图和Eclipse的视频不一样吧。但是确实可以引用到。像success.jsp就拿到了。效果图我没传。嘿嘿
如果想在D:\workspace\FileUpload\WebContent\upload看到,在web.xml文件里的servlet节点里servlet-class后配置里加上参数表示D:\workspace\FileUpload\WebContent\upload ,原因就是内部认识虚拟路径,是吧!
上传后 webroot下的upload文件夹里看不到,但是去上传的绝对路径也就是程序中的path:D:\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\FileUpload\upload 可以看到上传成功了,但是找到项目的路径D:\workspace\FileUpload\WebContent\upload是没有的,所以在项目里怎么刷新也看不到。老师说是一个是开发路径,一个是实际路径。直接用,是可以得到的,只是在项目里看不到。我看的视频,那个风中叶老师的struts2视频的第13讲,可以看到,可能是MyEclipse的视图和Eclipse的视频不一样吧。但是确实可以引用到。像success.jsp就拿到了。效果图我没传。嘿嘿
如果想在D:\workspace\FileUpload\WebContent\upload看到,在web.xml文件里的servlet节点里servlet-class后配置里加上参数表示D:\workspace\FileUpload\WebContent\upload ,原因就是内部认识虚拟路径,是吧!
#11
上传的组件很多,用起也很方便,struts就有自带的!
#12
mark
#13
#1
3.在Servlet中取得路径:
(1)得到工程目录:request.getSession().getServletContext().getRealPath("") 参数可具体到包名。
结果:E:\Tomcat\webapps\TEST
(2)得到IE地址栏地址:request.getRequestURL()
结果:http://localhost:8080/TEST/test
(3)得到相对地址:request.getRequestURI()
结果:/TEST/test
http://hi.baidu.com/java1016/blog/item/dfe50807964eac7c030881b6.html
(1)得到工程目录:request.getSession().getServletContext().getRealPath("") 参数可具体到包名。
结果:E:\Tomcat\webapps\TEST
(2)得到IE地址栏地址:request.getRequestURL()
结果:http://localhost:8080/TEST/test
(3)得到相对地址:request.getRequestURI()
结果:/TEST/test
http://hi.baidu.com/java1016/blog/item/dfe50807964eac7c030881b6.html
#2
学习
#3
request.getSession().getServletContext().getRealPath("/upload") 我用了这个 但是上传不到啊,也不报错。哪位高手指点啊
#4
request.getSession().getServletContext().getRealPath("/WEB-INF/upload")
#5
都不行啊,哪位大哥救命啊!!!
老师这样写的可以。 openStream ?为什么呢?怎么让传上去的文件名和之前一样的?
package com.cstp.upload;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
/**
* Servlet implementation class for Servlet: FileUpload1
*
*/
public class FileUpload1 extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
static final long serialVersionUID = 1L;
private String path;
public FileUpload1() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if(ServletFileUpload.isMultipartContent(request)){
FileItemFactory dfif=new DiskFileItemFactory();
System.out.println("=================================================");
PrintWriter w = response.getWriter();
ServletFileUpload dfu=new ServletFileUpload(dfif);
dfu.setHeaderEncoding("utf-8");
try {
FileItemIterator item = dfu.getItemIterator(request);
while(item.hasNext()){
FileItemStream obj = item.next();
System.out.print("是否为普通文本框:"+obj.isFormField()+"\t");
System.out.print("\n文本框的name属性:"+obj.getFieldName()+"\t");
System.out.println("\n文件类型:"+obj.getContentType());
if(!obj.isFormField()){
InputStream in = obj.openStream();
System.out.println("\n被上传文件路径:"+obj.getName());
if(obj.getName().trim().length()<1)
continue;
//File file=new File(path);
File file = File.createTempFile("temp_",obj.getName().substring(obj.getName().lastIndexOf('.')),new File(path));
OutputStream out = new FileOutputStream(file);
int i;
while((i=in.read())!=-1)
out.write(i);
System.out.println("\n存放文件的路径:"+file.getAbsolutePath());
out.close();
w.println("<img src='./upload/"+file.getName()+"'>");
}
System.out.println("-------------------------------------------------------------------------------");
}
} catch (FileUploadException e) {
e.printStackTrace();
}
}
}
@Override
public void init() throws ServletException {
super.init();
System.out.println(super.getServletContext().getRealPath("/upload"));
path=super.getInitParameter("path");
}
}
老师这样写的可以。 openStream ?为什么呢?怎么让传上去的文件名和之前一样的?
package com.cstp.upload;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
/**
* Servlet implementation class for Servlet: FileUpload1
*
*/
public class FileUpload1 extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
static final long serialVersionUID = 1L;
private String path;
public FileUpload1() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if(ServletFileUpload.isMultipartContent(request)){
FileItemFactory dfif=new DiskFileItemFactory();
System.out.println("=================================================");
PrintWriter w = response.getWriter();
ServletFileUpload dfu=new ServletFileUpload(dfif);
dfu.setHeaderEncoding("utf-8");
try {
FileItemIterator item = dfu.getItemIterator(request);
while(item.hasNext()){
FileItemStream obj = item.next();
System.out.print("是否为普通文本框:"+obj.isFormField()+"\t");
System.out.print("\n文本框的name属性:"+obj.getFieldName()+"\t");
System.out.println("\n文件类型:"+obj.getContentType());
if(!obj.isFormField()){
InputStream in = obj.openStream();
System.out.println("\n被上传文件路径:"+obj.getName());
if(obj.getName().trim().length()<1)
continue;
//File file=new File(path);
File file = File.createTempFile("temp_",obj.getName().substring(obj.getName().lastIndexOf('.')),new File(path));
OutputStream out = new FileOutputStream(file);
int i;
while((i=in.read())!=-1)
out.write(i);
System.out.println("\n存放文件的路径:"+file.getAbsolutePath());
out.close();
w.println("<img src='./upload/"+file.getName()+"'>");
}
System.out.println("-------------------------------------------------------------------------------");
}
} catch (FileUploadException e) {
e.printStackTrace();
}
}
}
@Override
public void init() throws ServletException {
super.init();
System.out.println(super.getServletContext().getRealPath("/upload"));
path=super.getInitParameter("path");
}
}
#6
是不是你的网站在war里,如果是这样的话 request.getSession().getServletContext().getRealPath 会返回 null
#7
我遇到的也是这样的情况 至今还没有解决
#8
可不可以直接部署网站,而不是不网站放到war包里。这样不就行了。
#9
到网上找一下,smartupload,fileupload的资料吧!
#10
问题解决了,实际上是2个环境的问题。一个开发环境,一个是实际环境。
上传后 webroot下的upload文件夹里看不到,但是去上传的绝对路径也就是程序中的path:D:\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\FileUpload\upload 可以看到上传成功了,但是找到项目的路径D:\workspace\FileUpload\WebContent\upload是没有的,所以在项目里怎么刷新也看不到。老师说是一个是开发路径,一个是实际路径。直接用,是可以得到的,只是在项目里看不到。我看的视频,那个风中叶老师的struts2视频的第13讲,可以看到,可能是MyEclipse的视图和Eclipse的视频不一样吧。但是确实可以引用到。像success.jsp就拿到了。效果图我没传。嘿嘿
如果想在D:\workspace\FileUpload\WebContent\upload看到,在web.xml文件里的servlet节点里servlet-class后配置里加上参数表示D:\workspace\FileUpload\WebContent\upload ,原因就是内部认识虚拟路径,是吧!
上传后 webroot下的upload文件夹里看不到,但是去上传的绝对路径也就是程序中的path:D:\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\FileUpload\upload 可以看到上传成功了,但是找到项目的路径D:\workspace\FileUpload\WebContent\upload是没有的,所以在项目里怎么刷新也看不到。老师说是一个是开发路径,一个是实际路径。直接用,是可以得到的,只是在项目里看不到。我看的视频,那个风中叶老师的struts2视频的第13讲,可以看到,可能是MyEclipse的视图和Eclipse的视频不一样吧。但是确实可以引用到。像success.jsp就拿到了。效果图我没传。嘿嘿
如果想在D:\workspace\FileUpload\WebContent\upload看到,在web.xml文件里的servlet节点里servlet-class后配置里加上参数表示D:\workspace\FileUpload\WebContent\upload ,原因就是内部认识虚拟路径,是吧!
#11
上传的组件很多,用起也很方便,struts就有自带的!
#12
mark