解决方案 »
- Connection aborted by peer 异常,求高手
- 如何在html页面中嵌入一个word文档
- Hibernate一对多,多对多的关系
- <c:if test="${<bean:write name="sortid"/>=='1'}">这么写为什么不对呢?
- tomcat集群的两个问题
- 100分求.......jsp方法为什么连接不了数据库??的解决方法
- Tomcat 启动时错误
- 在jsp中使用水晶报表的兄弟看过来
- 初学JSP........不会..快来帮帮..
- 我的j2sdkee 根本起不来??
- ssh参数传递问题,求大神帮忙啊,网上找了好久都没发现解决方案
- BS架构网站国际化的解决方案?
action:
// 文件操作
String realPath = FileUtil.getServicePathMk(FileUtil.COMPLAINTFILEPATH);
FileUtil.fileMoveTo(upfile,new File(realPath + "\\" + model.getcImage()));FileUtil:
import java.io.File;import org.apache.struts2.ServletActionContext;/**
* 文件操作类
* @author Daniel.Yim
*
*/
public class FileUtil {
//定义项目使用的文件路径
public static final String COMPLAINTFILEPATH="/files/complaint";
//Note 可以在此添加自己的文件路径
/**
* 获取服务器路径
*
* 依据参数指定的相对路径获取服务器的真实文件路径,路径可能会不存在。如果路径不存在则返回null
*
* @param path 定义的项目根目录的相对路径
* @return 真实的服务器路径
*/
public static String getServicePath(String path)
{
return ServletActionContext.getServletContext().getRealPath(path);
}
/**
* 获取服务器路径
*
* 依据参数指定的相对路径获取服务器的真实文件路径,如果路径不存在,则依据create参数进行创建
*
* @param path
* @param create
* @return
*/
public static String getServicePathMk(String path)
{
String serpath=getServicePath(path);
if(serpath==null){
serpath=ServletActionContext.getServletContext().getRealPath("/");
File pathFile=createFilePath(serpath+pathConvert(path,'/','\\'));
return pathFile.getAbsolutePath();
}
return serpath;
}
/**
* 路径字符串转换
*
* 将path中的字符c1转换为c2
*
* @param path
* @param c1
* @param c2
* @return
*/
public static String pathConvert(String path,char c1,char c2)
{
return path.replace(c1,c2);
}
public static void fileMoveTo(File srcFile,File toFile)
{
if(srcFile!=null&&toFile!=null)
{
createFilePath(toFile.getParent());
srcFile.renameTo(toFile);
srcFile.delete();
}
}
/**
* 创建文件路径
* @param file
*/
public static void createFilePath(File file)
{
if(!file.exists())
file.mkdirs();
}
public static File createFilePath(String path)
{
File file=null;
if(!StringUtil.IsEmpty(path))
{
file=new File(path);
createFilePath(file);
}
return file;
}
}