只能通过上传文件
jspsmartupload
struts也自己有上传类
jspsmartupload
struts也自己有上传类
解决方案 »
- JBOSS的配置文件问题
- 关于修改input type='hidden' 中的value值
- BOSS技术疑惑+散分
- 一个<html:options>标签问题
- 50分求适用于JSP的所见即所得编辑器
- 高分求:一个关于框架跳转的问题!
- 急:设置菜单项JMenuItem()的快捷键的疑问?为何有个"Ctrl-C" 而不是“Ctrl+C”
- 我想在网上搞个个人网页,怎么现在支持servlet的服务器这么少啊?
- java下载html文件的问题。急!!!
- jsp按钮怎样美化?
- 如何获得当前的日期格式如?20050102
- Tomcat服务器下JSP和JavaBean里使用<jsp:setProperty name="user1" property="*" />出现中文乱码,如何解决
This interface represents a file that has been uploaded by a client.
2.MultipartRequestHandler
MultipartRequestHandler provides an standard interface for struts to deal with file uploads from forms with enctypes of "multipart/form-data". 你可以通过这二个接口得到文件的二进制流。struts的example里只介绍了FormFile的用法,你可以去看看。MultipartRequestHandler的用法和commons-fileupload包有点类似。
<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />
声明一个SmartUpload 的实例
然后 mySmartUpload.initialize(pageContext); mySmartUpload.setTotalMaxFileSize(100000);
mySmartUpload.upload();//上传
for (int i=0;i<mySmartUpload.getFiles().getCount();i++){ com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i);
}//获得上传的文件!这个你详细看看jspsmartupload的操作!
---
结合得不错啊,呵呵
jspsmartupload
struts也自己有上传类
================================
不通过上传文件,不能读到客户端文件的2进制的流吗?
import java.io.IOException;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
public class ImageUploadAction extends Action {
public ActionForward execute(ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse)
{
ImageUploadForm imageUploadForm = (ImageUploadForm)actionForm;
ImageUpload imageUpload = new ImageUpload();
String pathId=(String)imageUploadForm.getPathId(); //前台<input>元素的ID
String imgId=(String)imageUploadForm.getImgId(); //前台<img> 图片显示元素的ID
FormFile theFile=imageUploadForm.getImagePath(); //图片路径
String fileName="none.jpg";
if(theFile!=null&&!theFile.equals("")){
String path=this.getServlet().getServletContext().getRealPath("upload/congress/picture");
fileName=imageUpload.getUName(theFile);
try {
//上传文件
imageUpload.upload(theFile,path,fileName);
} catch (FileNotFoundException e) {
httpServletRequest.setAttribute("exception","文件未找到!"+e);
return actionMapping.findForward("exception");
} catch (IOException e) {
httpServletRequest.setAttribute("exception","文件上传异常!"+e);
return actionMapping.findForward("exception");
}
}
httpServletRequest.setAttribute("pathId",pathId);
httpServletRequest.setAttribute("pathId",pathId);
httpServletRequest.setAttribute("pic",fileName);
return actionMapping.findForward("success");
}
}
import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;
/**
* <p>Title: 图片上传Form</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: 西安恩科网络科技有限公司</p>
* @author zhangyh
* @version 1.0
*/
public class ImageUploadForm extends ActionForm{
private FormFile imagePath; //图片路径
private String pathId; //前台<input>图片路径元素的ID
private String imgId; //前台<img> 图片显示元素的ID
/**
* @return Returns the imagePath.
*/
public FormFile getImagePath() {
return imagePath;
}
/**
* @param imagePath The imagePath to set.
*/
public void setImagePath(FormFile imagePath) {
this.imagePath = imagePath;
}
/**
* @return Returns the imgId.
*/
public String getImgId() {
return imgId;
}
/**
* @param imgId The imgId to set.
*/
public void setImgId(String imgId) {
this.imgId = imgId;
}
/**
* @return Returns the pathId.
*/
public String getPathId() {
return pathId;
}
/**
* @param pathId The pathId to set.
*/
public void setPathId(String pathId) {
this.pathId = pathId;
}
}
import org.apache.struts.upload.FormFile;import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ImageUpload { /**
* 图片上传
* @param formFile
* @param path
* @param uName
* @throws FileNotFoundException
* @throws IOException
*/
public void upload(FormFile formFile,String path,String uName) throws FileNotFoundException, IOException{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream stream = formFile.getInputStream();
OutputStream bos = new FileOutputStream(path+"/"+uName);
int bytesRead = 0;
byte buffer[] = new byte[8192];
while((bytesRead = stream.read(buffer, 0, 8192)) != -1)
bos.write(buffer, 0, bytesRead);
bos.close();
}
/**
* 获取文件上传后名称
* @param formFile
* @return
*/
public String getUName(FormFile formFile){
return this.uid()+this.postfix(formFile);
}
/**
* 文件数据库编号(别名)
* @return
*/
private String uid()
{
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddhhmmss");
Date d = new Date();
return format.format(d);
}
/**
* 文件后缀
* @param fileName
* @return
*/
public String postfix(FormFile formFile){
if (formFile != null && !formFile.getFileName().equals("")) {
String name = formFile.getFileName();
return name.substring(name.indexOf("."), name.length());
} else {
return null;
}
}
}