日期:2014-05-18  浏览次数:20765 次

求解struts2上传时文件接不到问题。
JSP <form action="uploadMusicAction!upload.action" enctype="multipart/form-data">
<div class="size">标题:<input type="text" name="title" class="uploadMusic"/></div>
<div>&emsp;</div>
<div class="size">文件:<s:file name="upload" cssClass="uploadMusic"/></div>
<div>&emsp;</div>
<div class="size2"><input type="submit" value="上传" class="uploadButton"></div>
</form>

struts.xml
[color=#00CCFF]  <action name="uploadMusicAction" class="uploadMusicSpring">
           <interceptor-ref name="fileUpload"> 
  <param name="allowedTypes"> 
            image/bmp,image/png,image/gif,image/jpeg,image/jpg,image/x-png, image/pjpeg 
            </param> 
           <!--  配置允许上传的文件大小,单位字节  -->
            <param name="maximumSize">1024000</param>
            </interceptor-ref> 
            <interceptor-ref name="defaultStack"/>
   <result name="uploadSuccess">/music/uploadMusic.jsp</result>
</action>

Java类
public class UploadMusicAction {

private String message;
private IUploadMusicService uploadMusicService;
private File upload;
private String uploadFileName;
private String uploadContentType;
private String title;

public String upload() throws IOException {
System.out.println("upload======"+upload);
System.out.println("title======"+title);
        String path = ServletActionContext.getServletContext().getRealPath("/upload");
        
        System.out.println(path);
        if(upload != null){
        File savefile = new File(new File(path),uploadFileName);
        if(!savefile.getParentFile().exists())
               savefile.getParentFile().mkdirs();
        try {
               FileUtils.copyFile(upload , savefile);
        } catch (IOException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
        }
       
        String[] t