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 {
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;
}
}