程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> JSP編程 >> 關於JSP >> Struts2 the Action upload file get json data

Struts2 the Action upload file get json data

編輯:關於JSP

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

}


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved