项目是ssh写的,代码要嵌套在action的java文件中去
public class UploadAction extends ActionSupport {
private static final long serialVersionUID = -6707209240028419956L;
private File uploadify;
private String sclx;//1.é®ä»¶
private String uploadifyFileName;
private static final DateFormat df = new SimpleDateFormat("yyyyMMddHHmm");
@Autowired
private WenjdetailManager wenjdetailManager;
public String uploadFile() throws Exception {
String extName = "";// æ©å±å
String newFileName = "";// æ°æ件å
String nowTime = df.format(new Date());// å½åæ¶é´
String random = "-" + (Math.round(Math.random() * 9000) + 1000);// éæºå½æ°
String path="";
if(sclx!=null&&sclx.equals("1"))
path = "uploadsyj/" + nowTime.substring(0, 6) + "/" + nowTime.substring(0, 8) + "/";// ä¿åè·¯å¾
else
path = "uploads/" + nowTime.substring(0, 6) + "/" + nowTime.substring(0, 8) + "/";// ä¿åè·¯å¾
String savePath = ServletActionContext.getServletContext().getRealPath("");
savePath = savePath.replace("\\", "/");
if (!savePath.substring(savePath.length()).equals("/"))
savePath = savePath + "/";
savePath = savePath + path;
// è·åæ©å±å
if (uploadifyFileName.lastIndexOf(".") >= 0) {
extName = uploadifyFileName.substring(uploadifyFileName.lastIndexOf("."));
}
newFileName = uploadifyFileName.substring(0, uploadifyFileName.lastIndexOf(".")) + nowTime.substring(8)
+ random + extName;
File file = new File(savePath);
if (!file.exists())
file.mkdirs();
uploadify.renameTo(new File(savePath + newFileName));
/*
* HttpServletResponse response = ServletActionContext.getResponse();
* response.setCharacterEncoding("utf-8");
* response.getWriter().print(uploadifyFileName+"ä¸ä¼ æå");
*/
String ctx=Struts2Utils.getRequest().getContextPath();
Struts2Utils.renderText(ctx+"/"+path + newFileName );
return null; // è¿éä¸éè¦é¡µé¢è½¬åï¼æ以è¿å空就å¯ä»¥äº
}
public File getUploadify() {
return uploadify;
}
public void setUploadify(File uploadify) {
this.uploadify = uploadify;
}
public String getUploadifyFileName() {
return uploadifyFileName;
}
public void setUploadifyFileName(String uploadifyFileName) {
this.uploadifyFileName = uploadifyFileName;
}
public String getSclx() {
return sclx;
}
public void setSclx(String sclx) {
this.sclx = sclx;
}
}
private static final long serialVersionUID = -6707209240028419956L;
private File uploadify;
private String sclx;//1.é®ä»¶
private String uploadifyFileName;
private static final DateFormat df = new SimpleDateFormat("yyyyMMddHHmm");
@Autowired
private WenjdetailManager wenjdetailManager;
public String uploadFile() throws Exception {
String extName = "";// æ©å±å
String newFileName = "";// æ°æ件å
String nowTime = df.format(new Date());// å½åæ¶é´
String random = "-" + (Math.round(Math.random() * 9000) + 1000);// éæºå½æ°
String path="";
if(sclx!=null&&sclx.equals("1"))
path = "uploadsyj/" + nowTime.substring(0, 6) + "/" + nowTime.substring(0, 8) + "/";// ä¿åè·¯å¾
else
path = "uploads/" + nowTime.substring(0, 6) + "/" + nowTime.substring(0, 8) + "/";// ä¿åè·¯å¾
String savePath = ServletActionContext.getServletContext().getRealPath("");
savePath = savePath.replace("\\", "/");
if (!savePath.substring(savePath.length()).equals("/"))
savePath = savePath + "/";
savePath = savePath + path;
// è·åæ©å±å
if (uploadifyFileName.lastIndexOf(".") >= 0) {
extName = uploadifyFileName.substring(uploadifyFileName.lastIndexOf("."));
}
newFileName = uploadifyFileName.substring(0, uploadifyFileName.lastIndexOf(".")) + nowTime.substring(8)
+ random + extName;
File file = new File(savePath);
if (!file.exists())
file.mkdirs();
uploadify.renameTo(new File(savePath + newFileName));
/*
* HttpServletResponse response = ServletActionContext.getResponse();
* response.setCharacterEncoding("utf-8");
* response.getWriter().print(uploadifyFileName+"ä¸ä¼ æå");
*/
String ctx=Struts2Utils.getRequest().getContextPath();
Struts2Utils.renderText(ctx+"/"+path + newFileName );
return null; // è¿éä¸éè¦é¡µé¢è½¬åï¼æ以è¿å空就å¯ä»¥äº
}
public File getUploadify() {
return uploadify;
}
public void setUploadify(File uploadify) {
this.uploadify = uploadify;
}
public String getUploadifyFileName() {
return uploadifyFileName;
}
public void setUploadifyFileName(String uploadifyFileName) {
this.uploadifyFileName = uploadifyFileName;
}
public String getSclx() {
return sclx;
}
public void setSclx(String sclx) {
this.sclx = sclx;
}
}
温馨提示:答案为网友推荐,仅供参考