81,092
社区成员
发帖
与我相关
我的任务
分享
public ActionForward upload(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws IOException {
System.out.println("当前路径:" + request.getContextPath());
CustomerForm cf = (CustomerForm) form;
FormFile formFile = cf.getExcelFile();
SessionContainer sc = SessionContainerUtils.getSessionContainer();
String upPath = "";
if (formFile != null) {
System.out.println();// 打印出实际上传时所选文件的文件名
String fileName = formFile.getFileName();
// 生成文件名为:当前登录帐号_原文件名_当前时间戳.原文件后缀
upPath = "/upload/" //************************* + sc.getLoginId()
+ "_"
+ fileName.substring(0, fileName.lastIndexOf("."))
+ "_"
+ System.currentTimeMillis()
+ fileName.substring(fileName.lastIndexOf("."), fileName
.length());
try {
FileOutputStream fos = new FileOutputStream(upPath);// 获取文件流对象
fos.write(formFile.getFileData());// 开始写入
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
这个是获取tomcat路径
private String getSavePath() {
PropertyResourceBundle res = (PropertyResourceBundle) ResourceBundle
.getBundle("Framework");
String tomcatPath = System.getProperty("catalina.home");
tomcatPath = tomcatPath.replace("/", File.separator) + "/webapps";
String savePath = tomcatPath + res.getString("upload");
savePath = savePath.replace("/", File.separator);
return savePath;
}
配置文件Framework.properties属性:
upload=/SMSServer/upload
request.getSession().getServletContext().getRealPath("/") + "upload/";