IO流上传下载

 1.首先是上传页面

FileUpload.jsp

 

<%@ page language="java" contentType="text/html; charset=utf-8"

    pageEncoding="utf-8"%>

<%@taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Insert title here</title>

</head>

<body>

<s:form action="ul" method="POST" enctype="multipart/form-data">

<s:file name="myFile" label="Image File">

<s:textfield name="caption" label="Caption"></s:textfield>

<s:submit>GO</s:submit>

</s:file>

</s:form>

</body>

</html>

2.通过struts配置关联Action,这里就不多介绍了。

 

package com.zc.text.zct.action;

 

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.Date;

 

import org.apache.struts2.ServletActionContext;

 

import com.opensymphony.xwork2.ActionSupport;

 

public class FileUpploadAction extends ActionSupport{

 

private static final long serialVersionUID = -622186839813885520L;

private static final int BUFFER_SIZE =16 * 1024;

private File myFile;//是对上传文件名称+路径进行编译后的串

private String contentType;//文件类型

private String fileName;//页面上传文件的名称

private String imageFileName;//新文件名称由当前时间和文件后缀名组成

private String caption;//标题说明

/**

 * 

 * TODO 通过流复制文件

 * @author :张驰

 * @param src

 * @param dst

 */

public static void copy(File src,File dst){

InputStream in = null;

OutputStream out = null;

try{

in = new BufferedInputStream(new FileInputStream(src),BUFFER_SIZE);

out = new BufferedOutputStream(new FileOutputStream(dst),BUFFER_SIZE);

try{

byte [] buffer = new byte [BUFFER_SIZE];

while(in.read(buffer)>0){

out.write(buffer);

}

out.flush();

}finally{

if(null != in){

in.close();

}

if(null != out){

out.close();

}

}

}catch (Exception e) {

// TODO: handle exception

}

}

/**

 * 

 * TODO 处理文件的名字

 * @author :张驰

 * @param fileName

 * @return

 */

public static String getExtention(String fileName){

int pos = fileName.lastIndexOf(".");

return fileName.substring(pos);

}

/**

 * 

 */

public String execute(){

imageFileName = new Date().getTime() + getExtention(fileName);

//File imageFile = new File(ServletActionContext.getServletContext().getRealPath(" /UploadImages" + "/" +imageFileName));

String filePath = ServletActionContext.getServletContext().getRealPath("UploadImages" + "/" +imageFileName);

//这里获取的是服务器的路径,可以打出来看下

System.out.println("系统路径:"+filePath);

File imageFile = new File(filePath);

copy(myFile,imageFile);

return "upload";

}

 

 

 

 

 

   public void setMyFileContentType(String contentType) {

         this .contentType = contentType;

    } 

    

      public void setMyFileFileName(String fileName) {

         this .fileName = fileName;

    }

 

 

public String getFileName() {

return fileName;

}

public void setFileName(String fileName) {

this.fileName = fileName;

}

public File getMyFile() {

return myFile;

}

public void setMyFile(File myFile) {

this.myFile = myFile;

}

public String getContentType() {

return contentType;

}

public void setContentType(String contentType) {

this.contentType = contentType;

}

public String getImageFileName() {

return imageFileName;

}

public void setImageFileName(String imageFileName) {

this.imageFileName = imageFileName;

}

public String getCaption() {

return caption;

}

public void setCaption(String caption) {

this.caption = caption;

}

 

 

}

3.ShowUpload.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<div style="padding: :3px; border: solid 1px #cccccc; text-align: center">
<img src="${pageContext.request.contextPath}/UploadImages/1350548284171.gif"/>
<br />
<s:property value="caption"/>
</div>
</body>
</html>

 

 

你可能感兴趣的:(上传下载)