java如何实现用iO流将文件从客户端拷贝到服务器端,是嵌套在web项目下的。求大侠,把源码贴出来 5
展开全部
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;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询