java如何实现用iO流将文件从客户端拷贝到服务器端,是嵌套在web项目下的。求大侠,把源码贴出来 5

项目是ssh写的,代码要嵌套在action的java文件中去... 项目是ssh写的,代码要嵌套在action的java文件中去 展开
 我来答
dahezi666
2012-08-22 · TA获得超过215个赞
知道答主
回答量:336
采纳率:13%
帮助的人:108万
展开全部
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;
}

}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式