网上搜的方法都不好用,不是在jsp和servelet中,是在后台类中。
大家帮帮忙。
大家帮帮忙。
解决方案 »
- 用UML怎么 表示这个关系图
- 郁闷呢,被客户投诉了,说java开发的二期还没有asp.net开发的一期快,慢很多啊!
- hibernate 查询语句 order by 的简单用法
- 真心请教:关于选座位买票的jsp页面要怎么做
- struts2的url乱码问题,action传值没有乱码。
- 获得hashtable的key
- 有人用Sun Java(TM) Studio Creator
- courseimport sql error:Cannot disable AUTO_COMMIT
- 【送分】BigDecimal SQL对应MONEY 类型赋值,在线等
- 关于EntityManager的createNativeQuery!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- 关于jsp中如何插入背景图片
- MyEclipse 版本之间有些什么区别
System.out.println(path);
這個方式是抓到你WebApp所在的路徑
* 得到本类物理路径所在文件夹
* @return
*/
private String getClassPath(){
String strClassName = getClass().getName();
String strPackageName = "";
if(getClass().getPackage() != null) {
strPackageName = getClass().getPackage().getName();
}
String strClassFileName = "";
if(!"".equals(strPackageName)){
strClassFileName = strClassName.substring(strPackageName.length() + 1,strClassName.length());
}
else {
strClassFileName = strClassName;
}
URL url = null;
url = getClass().getResource(strClassFileName + ".class");
String strURL = url.toString();
String middleString = System.getProperty("file.separator"); // 取得操作系统路径分割符
strURL = strURL.substring(strURL.indexOf( "/" ) + 1,strURL.lastIndexOf( "/" ));
return strURL;
}
一般来说
java.io.tmpdir不会被改变
方法1.
System.out.println(new File(System.getProperty("java.io.tmpdir")).getParentFile().getAbsolutePath());
方法2.取得Tomcat 的类,然后利用我如下文章的方法
http://blog.csdn.net/sunyujia/archive/2008/09/21/2957481.aspx
方法3.
在启动tomcat 的时候将%CD% 传递到jvm里面
import org.apache.struts2.util.ServletContextAware;import com.opensymphony.xwork2.ActionSupport;public class StrutsFileUpload extends ActionSupport implements
ServletContextAware { private File upload;// 实际上传文件 private String uploadContentType; // 文件的内容类型 private String uploadFileName; // 上传文件名 private String fileCaption;// 上传文件时的备注 private ServletContext context; public String execute() throws Exception { try {
String targetDirectory = context.getRealPath("/upload");
String targetFileName = uploadFileName;
File target = new File(targetDirectory, targetFileName);
FileUtils.copyFile(upload, target);
setUploadFileName(target.getPath());//保存文件的存放路径
} catch (Exception e) { addActionError(e.getMessage()); return INPUT;
} return SUCCESS; } public String getFileCaption() {
return fileCaption;
} public void setFileCaption(String fileCaption) {
this.fileCaption = fileCaption;
} public File getUpload() {
return upload;
} public void setUpload(File upload) {
this.upload = upload;
} public String getUploadContentType() {
return uploadContentType;
} public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
} public String getUploadFileName() {
return uploadFileName;
} public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
} public void setServletContext(ServletContext context) {
this.context = context;
}}
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>文件上传示例</title>
<link href="<s:url value="/css/main.css"/>" rel="stylesheet"
type="text/css" /> </head> <body> <s:actionerror />
<s:fielderror />
<s:form action="doUpload" method="POST" enctype="multipart/form-data">
<tr>
<td colspan="2">
<h1>
文件上传示例
</h1>
</td>
</tr> <s:file name="upload" label="上传的文件" />
<s:textfield name="fileCaption" label="备注" />
<s:submit value="上 传"/>
</s:form>
</body>
</html><%@ page language="java" contentType="text/html; charset=GB2312"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>上传成功</title>
<link href="<s:url value="/css/main.css"/>" rel="stylesheet"
type="text/css" />
</head> <body>
<table class="wwFormTable">
<tr> <td colspan="2">
<h1>
上传成功
</h1>
</td>
</tr> <tr>
<td class="tdLabel">
<label for="doUpload_upload" class="label">
内容类型:
</label>
</td>
<td>
<s:property value="uploadContentType" />
</td>
</tr> <tr>
<td class="tdLabel">
<label for="doUpload_upload" class="label">
文件路径:
</label>
</td>
<td>
<s:property value="uploadFileName" />
</td>
</tr>
<tr>
<td class="tdLabel">
<label for="doUpload_upload" class="label">
临时文件:
</label>
</td>
<td>
<s:property value="upload" />
</td>
</tr> <tr>
<td class="tdLabel">
<label for="doUpload_upload" class="label">
备注:
</label>
</td>
<td>
<s:property value="fileCaption" />
</td>
</tr>
</table> </body>
</html>
运行看看,不行,我这里有能运行的包,你要就给你传传
DirContext dirContext = DirContextURLStreamHandler.get();
//查询templates这个目录资源
Object obj = dirContext.lookup("/templates");
FileDirContext fdc;
String webrootPath;
// 判断取出来的对象是不是FileDirContext
// 也就是判断这个资源名对应的资源是不是目录资源
if (obj instanceof FileDirContext) {
fdc = (FileDirContext) obj;
// 获取web项目根目录的绝对路径
webrootPath = fdc.getDocBase();
}
String templatesPath = webrootPath + "/tempaltes"; 楼上很多答案都是牛头不对马嘴的,上面代码是网上查找的,具体结果还没测试 :)
参考文章的地址是http://mysun.javaeye.com/blog/453907