问题描述
- 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; }
}
解决方案
是不是页面没有添加传输文件的enctype属性
解决方案二:
Struts2上传文件代码
解决方案三:
报错代码呢。。。。。。。。。。。。。。。。。。。。。。。。。
解决方案四:
如果情况是没有报错而是每次都直接跳到input的话,就像楼上说的,你在action那里需要加一个东西。如果保存你就得看报错的位置和类型来解决了。