qq_36631950 2017-11-09 04:46 采纳率: 0%
浏览 3263
已结题

webuploader上传文件成功但是服务器拿不到文件

前端上传文件成功但是后台的action拿不到这个文件

前端代码我看是没有问题的 就是后台的我是下载过来用的

package com.sshoa.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import javax.servlet.ServletContext;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
public class Upload {
/**
* 普通批量上传文件方法
*
* @param fa
* 文件数组
* @param fna
* 文件名称数组
* @return 上传文件的路径字符串
/
public String commUpload(File[] fa, String[] fna) {
String paths = "";
ActionContext ac = ActionContext.getContext();
ServletContext sc = (ServletContext) ac.get(ServletActionContext.SERVLET_CONTEXT);
String rp = sc.getRealPath("/"), folder = "", newNm = "", ext = "";
// String rp = "/data/web/static/", folder = "", newName = "";
File dir = null, destFile = null;
InputStream is = null;
OutputStream os = null;
for(int i = 0; i < fa.length; ++i) {
ext = fna[i].substring(fna[i].lastIndexOf(".")).toLowerCase();
if(".jpg.png.bmp.gif".indexOf(ext) != -1) {
folder = newPath(1, rp);
} else if(".flv".equals(ext)) {
folder = newPath(2, rp);
} else {
folder = newPath(3, rp);
}
newNm = newName(fna[i]);
dir = new File(folder);
if(!dir.exists()) {
dir.mkdirs();
}
destFile = new File(dir, newNm);
try {
is = new FileInputStream(fa[i]);
os = new FileOutputStream(destFile);
byte[] buffer = new byte[400];
int length = 0;
while((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
paths += dir + newNm +",";
} catch(FileNotFoundException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
os.close();
} catch(IOException e) {
e.printStackTrace();
}
}
}
paths = paths.substring(0, paths.length()-1);
System.out.println(paths);
return paths;
}
/
*
* 单个上传文件方法
*
* @param fa
* 文件
* @param fna
* 文件名称
* @return 上传文件的路径
/
public String singleUpload(File fa, String fna) {
String path = "";
ActionContext ac = ActionContext.getContext();
ServletContext sc = (ServletContext) ac.get(ServletActionContext.SERVLET_CONTEXT);
String rp = sc.getRealPath("/"), folder = "", newNm = "", ext = "";
// String rp = "/data/web/static/", folder = "", newName = "";
File dir = null, destFile = null;
InputStream is = null;
OutputStream os = null;
ext = fna.substring(fna.lastIndexOf(".")).toLowerCase();
if(".jpg.png.bmp.gif".indexOf(ext) != -1) {
folder = newPath(1, rp);
} else if(".flv".equals(ext)) {
folder = newPath(2, rp);
} else {
folder = newPath(3, rp);
}
newNm = newName(fna);
dir = new File(folder);
if(!dir.exists()) {
dir.mkdirs();
}
destFile = new File(dir, newNm);
try {
is = new FileInputStream(fa);
os = new FileOutputStream(destFile);
byte[] buffer = new byte[400];
int length = 0;
while((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
path = dir + newNm;
} catch(FileNotFoundException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
os.close();
} catch(IOException e) {
e.printStackTrace();
}
}
return path;
}
/
*
* 上传文件路径
*
* @param fType
* 文件类型标识,1为图片,2为视频,3为其它文件
* @param rp
* 上传文件路径
* @return 上传文件的路径
/
public String newPath(int fType, String rp) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
String dts = sdf.format(new java.util.Date());
switch(fType) {
case 1 :
rp += "files/img/" + dts + "/l/";
break;
case 2 :
rp += "files/vdo/" + dts + "/";
break;
case 3 :
rp += "files/oth/" + dts + "/";
break;
default :
rp += "files/oth/" + dts + "/";
break;
}
return rp;
}
/
*
* 重命名上传文件
*
* @param srcName
* 上传文件的文件名
* @return 重命名后的文件名
*/
public String newName(String srcName) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String ext = srcName.substring(srcName.lastIndexOf(".")), dt = sdf.format(new java.util.Date()), rd = Math.round(Math.random() * 900) + 100 + "";
return dt + rd + ext;
}

}

package com.sshoa.action;
import java.io.File;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private File file;
private String fileFileName;
private String fileContentType;
private File[] up;
private String[] upFileName;
private String[] upContentType;
private int code;
private String msg;
public String ajaxUpload() {
System.out.println();
String ret = "input", ext = "";
if(file == null) {
code = 1000;
msg = "There is No File to Upload!";
} else {
ext = this.getFileFileName().substring(this.getFileFileName().lastIndexOf(".")).toLowerCase();
if(".jpg.png.bmp.gif.xls.xlsx.doc.docx.ppt.pptx.rar.zip.7z".indexOf(ext) != -1) {
Upload upload = new Upload();
String fps = upload.singleUpload(file, this.getFileFileName());
fps = fps.replace(",", "");
if(!"".equals(fps)) {
code = 1001;
msg = "Upload Files Success!";
ret = "success";
} else {
code = 1009;
msg = "Upload Files Failed!";
}
} else {
code = 1002;
msg = "The File of Type is not Allowed to Upload!";
}
}
return ret;
}
public String commUpload() {
String ret = "input", ext = "";
if(file == null) {
code = 1000;
msg = "There is No File to Upload!";
} else {
boolean ck = false;
for(int i=0; i ext = this.getUpFileName()[i].substring(this.getUpFileName()[i].lastIndexOf(".")).toLowerCase();
if(".jpg.png.bmp.gif.xls.xlsx.doc.docx.ppt.pptx.rar.zip.7z".indexOf(ext) == -1) {
ck = true;
break;
}
}
if(ck) {
code = 1002;
msg = "The Files which are not Allowed Type are Contained!";
} else {
Upload upload = new Upload();
String fps = upload.commUpload(up, this.getUpFileName());
fps = fps.replace(",", "");
if(!"".equals(fps)) {
code = 1001;
msg = "Upload Files Failed!";
ret = "success";
} else {
code = 1009;
msg = "Upload Files Failed!";
}
}
}
return ret;
}
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
public String getFileContentType() {
return fileContentType;
}
public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
}
public File[] getUp() {
return up;
}
public void setUp(File[] up) {
this.up = up;
}
public String[] getUpFileName() {
return upFileName;
}
public void setUpFileName(String[] upFileName) {
this.upFileName = upFileName;
}
public String[] getUpContentType() {
return upContentType;
}
public void setUpContentType(String[] upContentType) {
this.upContentType = upContentType;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
这是后台代码




  • 写回答

4条回答 默认 最新

  • 实力不会debug 2017-11-09 05:18
    关注
        public void 建议用这种格式存放代码,看的累,很少有人想看,技术大牛,本来就懒得回答你,现在更不会搭理你。(){
            System.out.print("新手路过");
            return;
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题