以前用cos作文件上傳
摘錄如下:
在用Java開發企業器系統的使用
首先申明
現在在國內用的非常多的一般是兩種方法解決來解決文件上傳
cos
這個是用的非常普遍的
UploadFile
<%@ page contentType="text/html;charset=gb
<head>
<title>fbysss UploadBean 示例</title>
<!
<!
</head>
<FORM name="form
<input name="title" type= "text" value="中文字">
<td class="bodystyle">附件</td>
<td class="bodystyle"> <input name="attach" type="FILE" id="attach" size="
<input name="ok" type= "submit" value="提交">
</form>
<!
//==========================================================================
//文件
//功能
//作者
//msn:jameslastchina@hotmail
//==========================================================================
<%@ page contentType="text/html;charset=GBK" %>
<%@ page language="java" import="com
<%@ page import="java
<%@ page import="java
<%@ page import="java
<%@ page import="javazoom
<%@ page import="uploadutilities
<head>
<meta http
</head>
<%
request
FileMover fileMover = new FileMover();//你也可以使用自帶的實例中jsp:useBean的形式
UploadBean upBean = new UploadBean();
MultipartFormDataRequest mrequest = null;
Hashtable files = null;
if (MultipartFormDataRequest
{
mrequest = new MultipartFormDataRequest(request
String sTt
out
String sTt
out
//這裡用來測試title參數是否正確
files = mrequest
}
//獲取修改前的文件名
String sOldFileName =mrequest
out
String sWebRootPath = request
String sPath=sWebRootPath+"attach";
int iFileCount =
String sServerFileName="";
String sLocalFileName = "";
//文件獲取
if ( (files != null) || (!files
iFileCount = files
UploadFile file = (UploadFile) files
sLocalFileName=file
out
int ii= sLocalFileName
String sExt = sLocalFileName
//得到不重復的文件名
java
SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmssSSS");
sServerFileName= fmt
sServerFileName =sServerFileName + sExt;
//如果不存在該目錄
File dir =new File(sPath);
if (!dir
dir
}
upBean
upBean
fileMover
upBean
out
}
%>
Demo注意事項
必備下載地址
UploadBean
FileMover
參考下載地址
ChinesUpload例子
參考了fbysss的一篇文章
Struts
選擇上傳文件頁面
<%@ taglib uri="/WEB
<html:html>
<html:form action="/uploadsAction
<html:file property="theFile"/>
<html:submit/>
</html:form>
</html:html>
UpLoadAction
import java
import javax
import org
import org
/** *//**
* <p>Title:UpLoadAction</p>
* <p>Description: QRRSMMS </p>
* <p>Copyright: Copyright (c)
* <p>Company: jiahansoft</p>
* @author wanghw
* @version
*/
public class UpLoadAction extends Action {
public ActionForward execute(ActionMapping mapping
ActionForm form
HttpServletRequest request
HttpServletResponse response)
throws Exception {
if (form instanceof uploadsForm) {//如果form是uploadsForm
String encoding = request
if ((encoding != null) && (encoding
{
response
}
UpLoadForm theForm = (UpLoadForm ) form;
FormFile file = theForm
try {
InputStream stream = file
String filePath = request
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream bos = new FileOutputStream(filePath + "/" +
file
//System
int bytesRead =
byte[] buffer = new byte[
while ( (bytesRead = stream
bos
}
bos
stream
}catch(Exception e){
System
}
//request
return mapping
}
return null;
}
}
UpLoadForm
import javax
import org
import org
/** *//**
* <p>Title:UpLoadForm</p>
* <p>Description: QRRSMMS </p>
* <p>Copyright: Copyright (c)
* <p>Company: jiahansoft</p>
* @author wanghw
* @version
*/
public class UpLoadForm extends ActionForm {
public static final String ERROR_PROPERTY_MAX_LENGTH_EXCEEDED = "org
protected FormFile theFile;
public FormFile getTheFile() {
return theFile;
}
public void setTheFile(FormFile theFile) {
this
}
public ActionErrors validate(ActionMapping mapping
{
ActionErrors errors = null;
//has the maximum length been exceeded?
Boolean maxLengthExceeded = (Boolean)
request
if ((maxLengthExceeded != null) && (maxLengthExceeded
{
errors = new ActionErrors();
errors
}
return errors;
}
}
//這是相對應的form
struts
<?xml version="
<!DOCTYPE struts
<struts
<form
<form
</form
<action
<action name="uploadsForm" type="UpLoadAction" path="/uploadsAction">
<forward name="display" path="/display
</action>
</action
</struts
<!
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/20075.html