upload.html
<html> <head> <title>上传文件</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <link rel="stylesheet" type="text/css" href="/ext-2.1/resources/css/ext-all.css"/> <script type="text/javascript" src="/ext-2.1/adapter/ext/ext-base.js"></script> <script type="text/javascript" src="/ext-2.1/ext-all.js"></script> <script type="text/javascript" src="/newExt/upload.js"></script> </head> <body> <h1>上传文件</h1> </body> </html>upload.js
Ext.onReady(function() { var form = new Ext.form.FormPanel({ fileUpload: true, baseCls: 'x-plain', layout:'absolute', url:'save-form.php', defaultType: 'textfield', frame:true, url: '/member/upload.action',// items: [{ xtype: 'textfield', fieldLabel: '文件名', name: 'upload', inputType: 'file'//文件类型 }], buttons: [{ text: '上传', handler: function() { form.getForm().submit({ success: function(form, action){ Ext.Msg.alert('信息', '文件上传成功!'); }, failure: function(){ Ext.Msg.alert('错误', '文件上传失败'); } }); } }] }); var window = new Ext.Window({ title: 'Resize Me', width: 500, height:300, minWidth: 300, minHeight: 200, layout: 'fit', plain:true, bodyStyle:'padding:5px;', buttonAlign:'center', items: form }); window.show(); });PhotoAction.java
public class PhotoAction extends SessionAwareAction { private static final long serialVersionUID = -3991411826094455715L; private File[] upload; private String[] uploadFileName; private String[] uploadContentType; private String[] dir; private String[] targetFileName; /**为上传文件自动分配文件名称,避免重复 * @param fileName * @return */ private String generateFileName(String fileName) { // 获得当前时间 DateFormat format = new SimpleDateFormat("yyMMddHHmmss"); // 转换为字符串 String formatDate = format.format(Calendar.getInstance().getTime()); // 随机生成文件编号 int random = new Random().nextInt(10000); // 获得文件后缀名称 int position = fileName.lastIndexOf("."); String extension = fileName.substring(position); // 组成一个新的文件名称 return formatDate + random + extension; } public String upload() throws IOException { // 获得upload路径的实际目录 @SuppressWarnings("unused") Member member = (Member)getSessionMap().get("member"); String realPath = "";//ServletActionContext.getServletContext().getRealPath("/image/" + member.getEmail() + "/" + (String)getSessionMap().get("selectAlbum")); //获得实际目录 String targetDirectory = "C://"; String[] mydir = new String[upload.length]; String[] tname = new String[upload.length]; for (int i = 0; i < upload.length; i++) { // 生成保存文件的文件名称 tname[i] = generateFileName(uploadFileName[i]); // 保存文件的路径 mydir[i] = targetDirectory + "//" + tname[i]; // 建立一个目标文件 File target = new File(targetDirectory, tname[i]); // 将临时文件复制到目标文件 try { FileUtils.copyFile(upload[i], target); } catch (IOException e) { this.setMessage(e.getMessage()); e.printStackTrace(); } } this.setMessage("上传" + upload.length + "张照片成功"); return SUCCESS; } public File[] getUpload() { return upload; } public void setUpload(File[] upload) { this.upload = upload; } public String[] getUploadFileName() { return uploadFileName; } public void setUploadFileName(String[] uploadFileName) { this.uploadFileName = uploadFileName; } public String[] getUploadContentType() { return uploadContentType; } public void setUploadContentType(String[] uploadContentType) { this.uploadContentType = uploadContentType; } public String[] getDir() { return dir; } public void setDir(String[] dir) { this.dir = dir; } public String[] getTargetFileName() { return targetFileName; } public void setTargetFileName(String[] targetFileName) { this.targetFileName = targetFileName; } }配置文件
<package name="com.hm.member.action" namespace="/member" extends="struts-default">
<action name="upload" class="photoAction" method="upload">