int star=value.lastIndexOf("\\");String filename=value.substring(star+1);//取文件名字System.out.println(filename);request.setAttribute(name, filename);item.write(new File(path,filename));
/* OutputStream os = new FileOutputStream(new File(path,filename));
String appName=request.getContextPath(); 项目的名称"../webapps"+appName+"/upload"
上传 很好用
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
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;public class UploadServlet extends HttpServlet { /* (non-Javadoc)
* @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {DiskFileItemFactory factory= new DiskFileItemFactory();
String path=request.getRealPath("/upload");//保存的默认路径,注意在Tomecate底下的
//System.out.println(path);
factory.setRepository(new File(path));
factory.setSizeThreshold(1024*1024);//上传的大于1M就存在磁盘里面
ServletFileUpload upload=new ServletFileUpload(factory);
try{
List<FileItem> list=upload.parseRequest(request);
//注意FileItem集合里面放的是提交的所有东西 包括username,password不仅仅是文件
for(FileItem item:list)
{
if(item.isFormField())//判断不是文件类型
{
String name=item.getFieldName();//取字段的名字
String value = item.getString("gbk");
request.setAttribute(name, value);
}
else//是文件类型
{
String name = item.getFieldName();
String value = item.getName();//只适用于返回文件类型的名字
int star=value.lastIndexOf("\\");String filename=value.substring(star+1);//取文件名字System.out.println(filename);request.setAttribute(name, filename);item.write(new File(path,filename));
/* OutputStream os = new FileOutputStream(new File(path,filename));
InputStream is = item.getInputStream();
byte[] buffer=new byte[400];
int length=0;
while((length = is.read(buffer))>0)
{
os.write(buffer,0,length);
}
os.close();
is.close();
*/
}
}}catch(Exception e)
{
e.printStackTrace();
}
request.getRequestDispatcher("upload/result2.jsp").forward(request, response);
}
}
常用的一个方法.反射URL url = getClass().getResource("GlobalValue.class");获取路径之后然后向上查找
如果要精准点项目路径的话可以这样:
String s=ServletActionContext.getServetContext.getRealPath("");
String path=s+"\\..\\temp"来定位到指定路径
但是工程下并没有建文件夹,我要放在当前项目下,而你说的只是下tomcat下建了一个文件下
如果要精准点项目路径的话可以这样:
String s=ServletActionContext.getServetContext.getRealPath("");
String path=s+"\\..\\temp"来定位到指定路径这也是在项目发布的tomcat下去寻找文件夹,而并不是在项目当前路径
System.out.println(f);
如果要精准点项目路径的话可以这样:
String s=ServletActionContext.getServetContext.getRealPath("");
String path=s+"\\..\\temp"来定位到指定路径这也是在项目发布的tomcat下去寻找文件夹,而并不是在项目当前路径
无语~~我刚刚通过这个方式做的补丁管理模块,结果到你这就不行了= =,你在开玩笑?
实在不行就定义一个java工具类。通过this.Class.getClassloader.getReasource();来获取class文件位置(别说这个不是在web-inf下面!!!)然后自己慢慢调位置
//获取项目的路径D:\workspace\demo
//demo 为工程的名称
System.out.println(f);
如果要精准点项目路径的话可以这样:
String s=ServletActionContext.getServetContext.getRealPath("");
String path=s+"\\..\\temp"来定位到指定路径这也是在项目发布的tomcat下去寻找文件夹,而并不是在项目当前路径
无语~~我刚刚通过这个方式做的补丁管理模块,结果到你这就不行了= =,你在开玩笑?
实在不行就定义一个java工具类。通过this.Class.getClassloader.getReasource();来获取class文件位置(别说这个不是在web-inf下面!!!)然后自己慢慢调位置
我做上传也是。怎么总是到tomcat下去了 webroot没有建根目录。全部跑到tomcat下去了