Struts2 the Action upload file get json data   发布时间:2017-09-12 15:07:17

Struts2 the Action upload file get json data

Java Code:


[java]
package com.shop.action;

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

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.shop.uitl.FileUitl;
import com.shop.uitl.ServerUrl;

public class FileUpload extends ActionSupport {

private File file; // 上传的文件
private String fileFileName; // 文件名称
private String fileContentType; // 文件类型
private String path; // 返回路径
private boolean status; // 状态
private String message; // 提示信息
private String url; //返回的url调用路径

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public void setFile(File file) {
this.file = file;
}

public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}

public String getFileFileName() {
return fileFileName;
}

public String getFileContentType() {
return fileContentType;
}

public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
}

public String getPath() {
return path;
}

public void setPath(String path) {
this.path = path;
}

public boolean isStatus() {
return status;
}

public void setStatus(boolean status) {
this.status = status;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

@Override
public String execute() throws Exception {
// TODO Auto-generated method stub

HttpServletRequest request=ServletActionContext.getRequest();
String path = ServerUrl.getServerPath(request, "upload");
String save=ServerUrl.getDiskPath(request, "upload");

if (file == null) {
this.setMessage("文件为空!");
status = false;
return SUCCESS;
}
try{
//创建目录 按照当天的日期来创建
Date date=new Date();
SimpleDateFormat format=new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat format2=new SimpleDateFormat("yyyyMMddhhmmss");
String strDate=format.format(date);
//检查文件夹是否存在
if(!new File(save+strDate).isDirectory()){
new File(save+strDate).mkdir();
}
//重命名文件

String name=FileUitl.getExtensionName(fileFileName);
fileFileName=format2.format(date)+name;

//写入到磁盘
FileInputStream in=new FileInputStream(file);
File outFile=new File(save+strDate+"\\"+fileFileName);
FileOutputStream out=new FileOutputStream(outFile);
//文件损坏
int ch=0;
while((ch=in.read())!=-1){
out.write(ch);
}
out.flush();
out.close();
in.close();

setMessage("文件上传成功!"); //响应信息
setPath("upload/"+strDate+"/"+fileFileName);//相对路径
setStatus(true); //状态
setUrl(ServerUrl.getServerPath(request, "upload/"+strDate+"/"+fileFileName)); //绝对路径

System.out.println(fileContentType + "已经接受到数据!");
return SUCCESS;
}catch (Exception e) {
setStatus(false);
setMessage("上传过程中发送了异常,采集软件,专题,上传失败!");
}

return SUCCESS;
}

}

package com.shop.action;

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

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.shop.uitl.FileUitl;
import com.shop.uitl.ServerUrl;

public class FileUpload extends ActionSupport {

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:武汉网站制作 https://www.feimao666.com