當前位置:首頁 » 文件管理 » 上傳enctype

上傳enctype

發布時間: 2022-12-15 13:31:40

① ajax上傳文件提交時,enctype=multipart/form-data怎麼帶過去

form中的欄位,加上get set方法

private FormFile file;

private String filename;

private String filesize;

action 部分:

public ActionForward execute(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response)

throws Exception {

String dir="D:/";

UpFileForm uff=(UpFileForm)form;

FormFile file=uff.getFile();

if(file.getFileSize()==0){

return mapping.findForward("success");

}

String fname=file.getFileName();

String size=Integer.toString(file.getFileSize())+"bytes";

InputStream streamIn=file.getInputStream();

OutputStream streamOut=new FileOutputStream(dir+"/"+fname);

int bytesRead=0;

byte[] buffer=new byte[8192];

while((bytesRead=streamIn.read(buffer,0,8192))!=-1){

streamOut.write(buffer,0,bytesRead);

}

streamOut.close();

streamIn.close();

uff.setFilename(fname);

uff.setFilesize(size);

file.destroy();

return mapping.findForward("success");

}

這樣將上傳的文件存在d盤。

② 前端上傳文件的幾種方法

1.表單上傳

最傳統的圖片上傳方式是form表單上傳,使用form表單的input[type=」file」]控制項,打開系統的文件選擇對話框,從而達到選擇文件並上傳的目的。

form表單上傳

表單上傳需要注意以下幾點:

(1).提供form表單,method必須是post。

(2).form表單的enctype必須是multipart/form-data。

javascript學習交流群:453833554

enctype 屬性規定在發送到伺服器之前應該如何對表單數據進行編碼。默認地,表單數據會編碼為 "application/x-www-form-urlencoded"。就是說,在發送到伺服器之前,所有字元都會進行編碼。HTML表單如何打包數據文件是由enctype這個屬性決定的。enctype有以下幾種取值:

application/x-www-form-urlencoded:在發送前編碼所有字元(默認)(空格被編碼為』+』,特殊字元被編碼為ASCII十六進制字元)。

multipart/form-data:不對字元編碼。在使用包含文件上傳控制項的表單時,必須使用該值。

text/plain:空格轉換為 「+」 加號,但不對特殊字元編碼。

默認enctype=application/x-www-form-urlencoded,所以表單的內容會按URL規則編碼,然後根據表單的提交方法:

method=』get』 編碼後的表單內容附加在請求連接後,

method=』post』 編碼後的表單內容作為post請求的正文內容。

③ form 在上傳文件時用enctype欄位有什麼用處

FORM元素的enctype屬性指定了表單數據向伺服器提交時所採用的編碼類型,默認的預設值是「application/x-www-form-urlencoded」。

這種編碼方式在表單發送之前都會將內容進行urlencode 編碼。(空格轉換為「+」,特殊字元轉化為ASCII的HEX值)。

比如我們在表單域中的

firstname填入 bb ,,

最後發送之前得到的結果就是: bb+%2C%2C

然而,在向伺服器發送大量的文本、包含非ASCII字元的文本或二進制數據時這種編碼方式效率很低。

在文件上載時,所使用的編碼類型應當是「multipart/form-data」,它既可以發送文本數據,也支持二進制數據上載。

瀏覽器端<form>表單的ENCTYPE屬性值為multipart/form-data,它告訴我們傳輸的數據要用到多媒體傳輸協議,由於多媒體傳輸的都是大量的數據,所以規定上傳文件必須是post方法,<input>的type屬性必須是file。

④ 文件上傳時加上enctype="multipart/form-data"獲取不到值

HashMapmap=newHashMap();StringdisplayImage="";//採用apache工具包進行文件上傳操作DiskFileItemFactoryfactory=newDiskFileItemFactory();ServletFileUploapload=newServletFileUpload(factory);//解析請求信息Listfileitems=upload.parseRequest(request);for(FileItemitem:fileitems){if(item.isFormField()){//簡單表單信息處理Stringname=item.getFieldName();Stringvalue=item.getString();//轉換下字元集編碼value=newString(value.getBytes("iso-8859-1"),"utf-8");map.put(name,value);}else{//原文件名Stringfilename=item.getName();if(filename.length()>0){StringdotName="";intindex=filename.lastIndexOf('.');if(index>0){dotName=filename.substring(index);}//生成新的文件名SimpleDateFormatsdf=newSimpleDateFormat("yyyyMMddHHmmss");filename=sdf.format(newDate())+dotName;displayImage="UploadFile/"+filename;Stringdir=application.getRealPath("UploadFile");System.out.print(dir);item.write(newFile(dir,filename));//刪除臨時文件item.delete();}}}

熱點內容
acfun如何上傳 發布:2025-08-13 07:35:10 瀏覽:270
ftp共享伺服器需要什麼配置 發布:2025-08-13 07:33:00 瀏覽:543
主要資料庫 發布:2025-08-13 07:15:27 瀏覽:178
壓縮包漫畫 發布:2025-08-13 07:15:25 瀏覽:131
伺服器空島原版如何獲得礦物 發布:2025-08-13 07:08:22 瀏覽:437
購車時哪些是必備的配置 發布:2025-08-13 06:42:33 瀏覽:693
寶塔添加腳本 發布:2025-08-13 06:41:56 瀏覽:502
ios資料庫存儲 發布:2025-08-13 06:28:10 瀏覽:975
java學習資源 發布:2025-08-13 06:07:56 瀏覽:395
傳奇地下宮殿腳本 發布:2025-08-13 06:07:21 瀏覽:956