java文件上传

package com.search.action.web; 

import java.io.*; 
import org.apache.struts2.ServletActionContext; 
import com.opensymphony.xwork2.ActionSupport; 

@SuppressWarnings("serial") 
public class UploadAction extends ActionSupport { 

private static final int BUFFER_SIZE = 16 * 1024; 
// 文件标题 
private String title; 
// 上传文件域对象 
private File upload; 
// 上传文件名 
private String uploadFileName; 
// 上传文件类型 
private String uploadContentType; 
// 保存文件的目录路径(通过依赖注入) 
private String savePath; 

// 自己封装的一个把源文件对象复制成目标文件对象 
private 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); 
byte[] buffer = new byte[BUFFER_SIZE]; 
int len = 0; 
while ((len = in.read(buffer)) > 0) { 
out.write(buffer, 0, len); 

} catch (Exception e) { 
e.printStackTrace(); 
} finally { 
if (null != in) { 
try { 
in.close(); 
} catch (IOException e) { 
e.printStackTrace(); 


if (null != out) { 
try { 
out.close(); 
} catch (IOException e) { 
e.printStackTrace(); 





@Override 
public String execute() throws Exception { 
System.out.println("aaaaa" + uploadFileName); 
System.out.println("bbbbb" + getUploadFileName()); 
// 根据服务器的文件保存地址和原文件名创建目录文件全路径 
String dstPath = ServletActionContext.getServletContext().getRealPath( 
this.getSavePath()) 
+ "\\" + this.getUploadFileName(); 
System.out.println("dstPath" + dstPath); 

System.out.println("上传的文件的类型:" + this.getUploadContentType()); 

File dstFile = new File(dstPath); 
copy(this.upload, dstFile); 
System.out.println("=========="); 
return "execute"; 


public String getTitle() { 
return title; 


public void setTitle(String title) { 
this.title = title; 


public File getUpload() { 
return upload; 


public void setUpload(File upload) { 
this.upload = upload; 


public String getUploadFileName() { 
return uploadFileName; 


public void setUploadFileName(String uploadFileName) { 
this.uploadFileName = uploadFileName; 


public String getUploadContentType() { 
return uploadContentType; 


public void setUploadContentType(String uploadContentType) { 
this.uploadContentType = uploadContentType; 


public String getSavePath() { 
return savePath; 


public void setSavePath(String savePath) { 
this.savePath = savePath; 















package com.bean; 

import java.io.*; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.ServletInputStream; 

public class UpBean { 
public void doUpload(HttpServletRequest request) throws IOException { 
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter( 
"test.txt"))); 
ServletInputStream in = request.getInputStream(); 
int i = in.read(); 
while (i != -1) { 
pw.print((char) i); 
i = in.read(); 

pw.close(); 






我写的是进入Action里面.去执行上传操作.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值