必须添加jar包commons-fileupload.jar,commons-io-2.0.1.jar
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>Struts2 File Upload</title> <script language="javascript" type="text/javascript"> function checked(){ if (!/\.(mp3|WMA)$/.test(document.getElementById("upload").value)) { alert("文件类型必须是.MP3.WMA中的一种"); document.getElementById("upload").value = ""; return false; } } </script> </head> <body> <s:text name=""></s:text> <form name="myform" action="fileUpload.action" method="post" enctype="multipart/form-data" οnsubmit="return checked();"> 文件标题:<s:textfield name="title"></s:textfield><br> 选择文件:<s:file name="upload" id="upload"></s:file> <input type="submit" value="上传"> </form> </body> </html>*******************************************
package com.song.action;
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.OutputStreamWriter; import org.apache.commons.io.FileUtils; public class UploadFileAction extends BaseAction { public static String FILESAVEPATH = "D:/Downloads"; /** * */ private static final long serialVersionUID = 1L; private String fileName; private String title; private File upload; @Override public String execute() throws Exception { String targetDirectory = FILESAVEPATH + File.separator; System.out.println("targetDirectory" + targetDirectory); System.out.println(fileName + "1111"); File target = new File(targetDirectory, fileName); System.out.println("111111" + FILESAVEPATH + "/upload.txt"); BufferedWriter bww = null; try { bww = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(FILESAVEPATH + "/upload.txt", true), "UTF-8")); File file = new File(FILESAVEPATH + "/upload.txt"); FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); String str = br.readLine(); System.out.println("str" + str); StringBuffer s = new StringBuffer(); String song = fileName + title; System.out.println("song" + song); if (str == null || "".equals(str)) { s.append(fileName).append("-").append(title); bww.write(s.toString().trim()); bww.newLine(); bww.flush(); } else if (!str.equals(song)) { s.append(fileName).append("-").append(title); bww.write(s.toString().trim()); bww.newLine(); bww.flush(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { freeIO(bww); } FileUtils.copyFile(upload, target); return SUCCESS; } public void freeIO(BufferedWriter bww) { try { if (bww != null) bww.close(); } catch (IOException e) { e.printStackTrace(); } } public String getTitle() { return title; } public File getUpload() { return upload; } public void setTitle(String title) { this.title = title; } public void setUpload(File upload) { this.upload = upload; } public void setUploadFileName(String fileName) { this.fileName = fileName; } }**************struts2.xml文件配置省略。。