kunni12345 2016-05-23 11:10 采纳率: 0%
浏览 1361
已结题

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

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条回答

  • intepter 2016-05-23 11:56
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 一道python难题2
  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备