首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

用struts2下传文件

2012-11-18 
用struts2上传文件在jsp页面:在form表单中一定要加上enctypemultipart/form-data???? 上传文件:input

用struts2上传文件

在jsp页面:在form表单中一定要加上enctype="multipart/form-data"
???? 上传文件:<input type="file" name="upload"/>?
? 在action页面:
? ?private File upload;
??private String uploadContentType;//文件类型注意此名的前半部分一定要和jsp中的file 的name一样
??private String uploadFileName;
??private String uploadSavepath="\\upload\\exam";?
??public String getUploadSavepath() {
??return ServletActionContext.getServletContext().getRealPath(this.uploadSavepath);
??}
??public void setUpload(File upload) {
???this.upload = upload;
??}
??public void setUploadContentType(String uploadContentType) {
???this.uploadContentType = uploadContentType;
??}
??public void setUploadFileName(String uploadFileName) {
???this.uploadFileName = uploadFileName;
??}
??
??public String uploadfile(){
?? if(this.upload != null){
????SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddhhmmss");
????String date = dateFormat.format(new Date());
????int simple = new Random().nextInt(1000);
????this.filename = date + simple + this.uploadFileName.substring(this.uploadFileName.indexOf("."));
????String bookAllName = this.getUploadSavepath() + File.separator + filename;
????File? file = new File(bookAllName);
????this.upload.renameTo(file);
???}
?? return "uploadfile";
? }
? ok了!

热点排行