Struts2上传文件失败,这段代码哪里有错? 1C

package com.Music.user;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class uploadmusic extends ActionSupport {
private File upload;
private String uploadContentType;
private String uploadFileName;
private String savePath;
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
public void setSavePath(String value) {
this.savePath = value;
}
@SuppressWarnings("deprecation")
private String getSavePath() throws Exception {
return ServletActionContext.getServletContext().getRealPath(savePath);
}

public String execute() throws Exception {
    ServletActionContext.getResponse().setCharacterEncoding("GB2312");
    PrintWriter out = ServletActionContext.getResponse().getWriter();
    ServletActionContext.getResponse().setHeader("Pragma", "No-cache");
    ServletActionContext.getResponse().setHeader("Cache-Control",
            "no-cache");
    ServletActionContext.getResponse().setDateHeader("Expires", 0);

    //获取文件后缀
    String fileType = getUploadFileName().substring(getUploadFileName().lastIndexOf("."));
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    Date dt = new Date();
    Random rd = new Random();
    setUploadFileName(sdf.format(dt) + rd.nextInt(9999) + fileType);

    if ("audio/mpeg".equals(getUploadContentType())) {
        FileOutputStream fos = new FileOutputStream(getSavePath() + "\\"
                + getUploadFileName());
        FileInputStream fis = new FileInputStream(getUpload());
        byte[] buffer = new byte[10240];
        int len = 0;
        while ((len = fis.read(buffer)) > 0) {
            fos.write(buffer, 0, len);
        }
        String filePath = "upload\\\\" + getUploadFileName();
        out.println(function.PlutoJump("上传成功,请认真填写歌曲内容!", "upload.jsp?path=" + filePath));

    } else {
        out.println(function.PlutoJump("文件类型必须为MP3!", "uploadmusic.jsp"));
    }
    return null;
}

}

3个回答

是不是页面没有添加传输文件的enctype属性

报错代码呢。。。。。。。。。。。。。。。。。。。。。。。。。

如果情况是没有报错而是每次都直接跳到input的话,就像楼上说的,你在action那里需要加一个东西。如果保存你就得看报错的位置和类型来解决了。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问