java发送url请求进行文件的提交及后台struts2的action接收处理
作者:网络转载 发布时间:[ 2013/7/2 10:42:39 ] 推荐标签:
3、后台服务端对应的struts2进行文件内容的接收处理【ajaxUploadFile和ajaxUploadFileTwo两个action的接收处理】
package com.eshopmates.finance.action;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.Action;
/**
* @作者 王建明
* @创建日期 2013-06-27
* @创建时间 18:22:33
* @版本号 V 1.0
*/
public class GetPostFileAction extends BaseAction {
// 上传文件
private File upFile;// 拦截器会为你在缓冲区创建临时文件,这是临时文件对象
private String upFileContentType;// 头域中的值
private String upFileFileName;// 报文体中的name
/**
* @return
* @作者 王建明
* @创建日期 2013-06-27
* @创建时间 19:26:22
* @描述 —— 网页表单方式或者模拟表单方式提交file文件进行处理
*/
@org.apache.struts2.convention.annotation.Action("ajaxUploadFile")
public String ajaxUploadFile() {
String result;
try {
String path = getRequest().getSession().getServletContext()
.getRealPath("/uploadFile/" + upFileFileName);// 路径
File currFile = new File(path);
System.out
.println("接收到的文件存放路径======>" + currFile.getAbsolutePath());
FileUtils.copyFile(this.upFile, currFile);// struts2提供的工具类,意思是把缓存区文件放到哪里
result = "{"success":true,"uploadFile":"" + currFile.getAbsolutePath()
+ "","fileSize":" + currFile.length() + "}";
} catch (IOException e) {
e.printStackTrace();
result = "{"success":false}";
}
super.ajaxPrintMsg(result, super.CONTENTTYPE_HTML);
System.out.println("result========>" + result);
return Action.NONE;
}
/**
* @return
* @throws Exception
* @作者 王建明
* @创建日期 2013-06-27
* @创建时间 19:26:55
* @描述 —— 直接以文件流的形式进行文件的post提交
*/
@org.apache.struts2.convention.annotation.Action("ajaxUploadFileTwo")
public String ajaxUploadFileTwo() throws Exception {
String result;
HttpServletRequest request = ServletActionContext.getRequest();
String rootPath = request.getSession().getServletContext().getRealPath(
"/");
String filePath = request.getParameter("filePath");
String fileName = request.getParameter("fileName");
System.out.println("fileName=====>" + fileName);
InputStream input = request.getInputStream();
String fileFullPath = rootPath + filePath + fileName;
File saveFile = new File(fileFullPath);
File file = new File(rootPath + filePath);
if (!file.exists()) {
file.mkdirs();
}
FileOutputStream fos = new FileOutputStream(fileFullPath);
int size = 0;
byte[] buffer = new byte[1024];
while ((size = input.read(buffer, 0, 1024)) != -1) {
fos.write(buffer, 0, size);
}
fos.close();
input.close();
result = "{"success":true,"uploadFileName":""
+ saveFile.getAbsolutePath() + "","fileSize":"
+ saveFile.length() + "}";
super.ajaxPrintMsg(result, super.CONTENTTYPE_HTML);
System.out.println("filePath===>" + file.getAbsolutePath());
return Action.NONE;
}
public File getUpFile() {
return upFile;
}
public void setUpFile(File upFile) {
this.upFile = upFile;
}
public String getUpFileContentType() {
return upFileContentType;
}
public void setUpFileContentType(String upFileContentType) {
this.upFileContentType = upFileContentType;
}
public String getUpFileFileName() {
return upFileFileName;
}
public void setUpFileFileName(String upFileFileName) {
this.upFileFileName = upFileFileName;
}
}

sales@spasvo.com