有文件上传的例子吗??
解决方案 »
- fckeditor 绑定textarea后获取textarea的值
- Asp中selectm=request.QueryString("selectm"); selectkey=trim(request(trim("selectkey")))
- 求助:网页打印问题
- 很简单的一个问题
- 在页面中读取指定的doc文件然后在IE中打开??
- JSP与html数据传输的问题,十万火急!!!
- 请教 jsp中 cookie 的问题
- 求解释
- 通过JDBC:ODBC访问SQL SERVER 2000的问题
- 文档根元素 "beans" 必须匹配 DOCTYPE 根 "null"
- 恶域名指向主机正常访问,而自己的域名访问出现异常
- ext树形菜单级联问题
把表单封装成form对象,在 Action里从form里面读取。
form代码:
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.hhaccp.hr.web.form;
import org.apache.struts.action.ActionForm;import org.apache.struts.upload.FormFile;/**
* MyEclipse Struts
* Creation date: 04-10-2010
*
* XDoclet definition:
* @struts.form name="pictureForm"
*/
public class PictureForm extends ActionForm {
/*
* Generated Methods
*/ /**
*
*/
private static final long serialVersionUID = -7396780480884393201L; /**
* Method validate
* @param mapping
* @param request
* @return ActionErrors
*/
private FormFile pictureForm; public FormFile getPictureForm() {
return pictureForm;
} public void setPictureForm(FormFile pictureForm) {
this.pictureForm = pictureForm;
}}
Action代码:
String pictureForm=request.getParameter("pictureForm");
if(pictureForm!=null){
PictureForm pic=(PictureForm) form;
byte[] file=pic.getPictureForm().getFileData();//把路径变成文件流对象
String pathName=request.getRealPath("/")+"file/head/";//创建上传路径
String picName=pic.getPictureForm().getFileName();//获取图片路径
File saveFile=new File(pathName+picName);//创建文件
OutputStream os=new FileOutputStream(saveFile);//把文件添加进文件流
os.write(file);//写入文件
os.close();//关闭文件流
humanFile.setPicture(pic.getPictureForm().getFileName());
}else{
humanFile.setPicture("dfhead.jpg");
}我的是上传图片!!!
package action;import java.io.*;import com.opensymphony.xwork2.ActionSupport;public class upload extends ActionSupport {
private File upload;
private String uploadFileName; //固定的名字不能乱起
private String uploadContentType;//固定的名字不能乱起
public String execute() throws IOException{
InputStream is = null;
OutputStream os = null;
byte buffer[] = new byte[8192];
int count=0;
try{
is = new FileInputStream(upload);
os = new FileOutputStream("d:\\java\\"+uploadFileName);
while((count=is.read(buffer))>0){
os.write(buffer,0,count);
}
}catch(Exception e){
e.printStackTrace();
}finally{
os.close();
is.close();
}
return SUCCESS;
}
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
//public String getFileName() {
// return fileName;
//}
//public void setFileName(String fileName) {
// this.fileName = fileName;
//}
public String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}public String getUploadFileName() {
return uploadFileName;
}public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
}upload.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'upload.jsp' starting page</title>
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head>
<body>
<s:form action="upload" namespace="/" method="post" enctype ="multipart/form-data">
<s:file name="upload" label="请输入要上传的文件"></s:file>
<s:submit value="上传"></s:submit>
</s:form>
</body>
</html>
测试成功.
http://download.csdn.net/source/1574104
struts1.2上传图片源代码(包含检查文件格式、限制文件大小功能)
/****************************************************************************
*更新产品类型图片 xfc
****************************************************************************/
public ActionForward updateWproducttypePic(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)throws Exception {
WproducttypeForm wproducttypeForm = (WproducttypeForm)form;
Integer id = wproducttypeForm.getId();
if(id!=null && id!=0){
Wproducttype wproducttype = wproducttypeBiz.findWproducttypeByKey(id);
//检测是否已有图,有就首先删除原来的图片,没有则直接保存
File file = new File(request.getRealPath("/upload/img/producttype/"+wproducttype.getUuidFileName()));
if(file.exists()){
file.delete();
System.out.println("-----------已经删除原来的类图");
}
FormFile filePic = null;
Integer indexId = wproducttypeForm.getIndexId();
if(indexId == 1) filePic = wproducttypeForm.getFile1();
if(indexId == 2) filePic = wproducttypeForm.getFile2();
if(indexId == 3) filePic = wproducttypeForm.getFile3();
if(indexId == 4) filePic = wproducttypeForm.getFile4();
if(indexId == 5) filePic = wproducttypeForm.getFile5();
if(indexId == 6) filePic = wproducttypeForm.getFile6();
//这里处理图片保存
String fileName = UUIDHelper.getUUID()+"."+FileHelper.getFileSuffix(filePic.getFileName());
boolean result = false;
String filePath = request.getRealPath("/upload/img/producttype/"+fileName);
try {
FileOutputStream fileOutput = new FileOutputStream(filePath);
fileOutput.write(filePic.getFileData());
fileOutput.flush();
fileOutput.close();
result = true;
} catch (Exception e) {
e.printStackTrace();
result=false;
}
if(result){
wproducttype.setUuidFileName(fileName);
wproducttype.setFileName(filePic.getFileName());
boolean result2 = wproducttypeBiz.updateWproducttype(wproducttype);
if(result2)reportErrors(request, "<font color='blue'>更新成功!</font>",false);
else reportErrors(request, "<font color='red' >更新失败!</font>",false);
}
}else{
reportErrors(request, "<font color='red'>对不起,您要数据不存在!或者数据已丢失!</font>",false);
}
return findWproducttypePage(mapping, form, request, response);
}