程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> struts2上傳文件

struts2上傳文件

編輯:C++入門知識

struts2上傳文件


1.upload.java(java文件上傳的讀寫方法)

package com.OS.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.struts2.ServletActionContext;

import com.sql.bean.FILE;

import com.opensymphony.xwork2.ActionSupport;

public class upload extends ActionSupport {
	private File picture;
	private String pictureContentType;
	private String pictureFileName;
	FILE file=new FILE();

	public FILE upload() throws Exception{
		//最好保護文件時重命名加時間截
		File saved = new File(ServletActionContext.getServletContext().getRealPath("upload"),pictureFileName);
		InputStream ins=null;
		OutputStream ous=null;
		
		try{
		saved.getParentFile().mkdirs();
		ins=new FileInputStream(picture);
		ous=new FileOutputStream(saved);
		
		byte[] b= new byte[1024];
		int len =0;
		while((len=ins.read(b))!=-1){
			ous.write(b,0,len);
		}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(ous!=null) ous.close();
			if(ins!=null) ins.close();
		}
		file.setFile(picture);
		file.setFileContentType(pictureContentType);
		file.setFileFileName(pictureFileName);
		
      //  BufferedReader bf = new BufferedReader(new StringReader(str));
		return file;
	}
	public File getPicture() {
		return picture;
	}
	public void setPicture(File picture) {
		this.picture = picture;
	}
	public String getPictureContentType() {
		return pictureContentType;
	}
	public void setPictureContentType(String pictureContentType) {
		this.pictureContentType = pictureContentType;
	}
	public String getPictureFileName() {
		return pictureFileName;
	}
	public void setPictureFileName(String pictureFileName) {
		this.pictureFileName = pictureFileName;
	}

}


2.FILE.java (文件上傳類)

package com.sql.bean;

import java.io.File;

public class FILE {
	private File file;
	private String fileContentType;
	private String fileFileName;
	public File getFile() {
		return file;
	}
	public void setFile(File file) {
		this.file = file;
	}
	public String getFileContentType() {
		return fileContentType;
	}
	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}
	public String getFileFileName() {
		return fileFileName;
	}
	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}
	
	
}

3.uploads.java (action方法)

package com.web.actoin;

import org.apache.struts2.ServletActionContext;

import com.OS.util.upload;
import com.opensymphony.xwork2.ActionSupport;

public class uploads extends ActionSupport{
	public upload u=new upload();
	public String uploadimage() throws Exception{
		System.out.println(".."+ServletActionContext.getRequest().getContextPath()+"/upload/"+u.upload().getFileFileName());
		return "success";
	}
	public upload getU() {
		return u;
	}
	public void setU(upload u) {
		this.u = u;
	}
	
}

4.gMessages.properties(消息文件)

struts.messages.error.uploading = \u6587\u4EF6\u4E0D\u80FD\u4E0A\u4F20\u7684\u901A\u7528\u9519\u8BEF\u4FE1\u606F
struts.messages.error.file.too.large = \u4E0A\u4F20\u6587\u4EF6\u957F\u5EA6\u8FC7\u5927\u7684\u9519\u8BEF\u4FE1\u606F
struts.messages.error.content.type.not.allowed =\u5F53\u4E0A\u4F20\u6587\u4EF6\u4E0D\u7B26\u5408\u6307\u5B9A\u7684contentType

5.struts.properties(struts2資源文件)

struts.custom.i18n.resources=gMessages

6.struts.xml(struts2配置文件)







        
     		
 		  		/jsp/  
                
                image/bmp,image/png,image/gif,image/jpeg
                
                1025956
            
            
            
		
    	index.jsp
    	success.jsp
    

    

7.index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    
	
	
	    
  
  
  



  



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