日期:2014-05-18  浏览次数:20997 次

javaBean上传文件如何写?
<jsp:useBean   id= "su "   scope= "page "   class= "com.jspsmart.upload.SmartUpload "   />
<%
su.initialize(pageContext);
su.upload();
int   count   =   su.save( "/upload ",   su.SAVE_VIRTUAL);
out.println(count+ "个文件上传成功! <br> ");
for   (int   i=0;i <su.getFiles().getCount();i++)
{
com.jspsmart.upload.File   file   =   su.getFiles().getFile(i);
if   (file.isMissing())   continue;
out.println( " <br> 文件名: "   +   file.getFileName()+ " 长度: "+file.getSize());
}
%>


以上代码如何写成javabean如下形式

public   boolean   upload()   throws   Exception   {
        .................................
                }

------解决方案--------------------
给大家推荐个

好的技术群  大家一起学习啊

32759197
------解决方案--------------------
import com.jspsmart.upload.SmartUpload;

public boolean upload() throws Exception {
HttpServletRequest request = null;
HttpServletResponse response = null;
JspFactory _jspxFactory = null;
PageContext pageContext = null;
_jspxFactory = JspFactory.getDefaultFactory();
pageContext = _jspxFactory.getPageContext(this, request, response, " ", true,
8192, true);
SmartUpload su = new SmartUpload();
su.initialize(pageContext);
su.upload();
int count = su.save( "/upload ", su.SAVE_VIRTUAL);
out.println(count + "个文件上传成功! <br/> ");
for (int i = 0; i < su.getFiles().getCount(); i++) {
com.jspsmart.upload.File file = su.getFiles().getFile(i);
if (file.isMissing())
continue;
out.println( " <br/> 文件名: " + file.getFileName() + " 长度: " + file.getSize());
}
}

------解决方案--------------------
String fName=file.getFileName();
InputStream fileStreamIn=file.getInputStream();
OutputStream fileStreamOut=new FileOutputStream(dir+ "/ "+fileName);//dir为上传后文件存放的地址

int bytesRead=0;
byte[]buffer=new byte[8192];
whie((bytesRead=fileStreamIn.read(buffer,0,8192))!=-1){
fileStreamOut.write(buffer,0,btyesRead);
}
fileStreamOut.close();
fileStreamIn.close();
以上程序载至Struts设计P320,
我的总结:流中读,流中写
------解决方案--------------------
在struts框架下定义一个Servlet,代码如下
package jspupload;

import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspFactory;
import javax.servlet.jsp.PageContext;

import com.jspsmart.upload.File;
import com.jspsmart.upload.SmartUpload;
/**
* @author user
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
public class UploadServlet extends HttpServlet{
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException{
try{
<