當前位置:首頁 » 文件管理 » java表單提交文件上傳

java表單提交文件上傳

發布時間: 2022-02-23 21:47:18

java文件上傳的幾種方式

1、通過Servlet類上傳
2、通過Struts框架實現上傳
ITJOB學到兩種方式

❷ SpringMVC表單提交時,多文件上傳和單個文件上傳有些什麼區別

基於Spring3 MVC實現基於form表單文件上傳
一:雜項准備
環境搭建參考這里-http://blog.csdn.net/jia20003/article/details/8471169
二:前台頁面
根據RFC1867,只要在提交form表單中聲明提交方法為POST,enctype屬
性聲明為multipart/form-data, action聲明到要提交的url即可。具體如下:

三:spring配置
使用spring3的MultipartHttpReqest來接受來自瀏覽器的發送的文件內容。
需要配Multipart解析器在express-servlet.xml中。內容如下:

同時還需要在maven的pom.xml文件添加apachefileupload與common-io兩個包。

四:Controller中方法實現

[java] view plain
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
public ModelAndView getUploadFile(HttpServletRequest request, HttpServletResponse response) {
System.out.println("fucking spring3 MVC upload file with Multipart form");
String myappPath = request.getSession().getServletContext().getRealPath("/");
try {
if (request instanceof MultipartHttpServletRequest) {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
System.out.println("fucking spring3 MVC upload file with Multipart form");
// String myappPath = multipartRequest.getServletContext().getRealPath("/");
// does not work, oh my god!!
MultipartFile file = multipartRequest.getFiles("userfile1").get(0);
long size = file.getSize();
byte[] data = new byte[(int) size];
InputStream input = file.getInputStream();
input.read(data);

// create file, if no app context path, will throws access denied.
// seems like you could not create any file at tomcat/bin directory!!!
File outFile = new File(myappPath + File.separator + file.getOriginalFilename());
if(!outFile.exists()) {
outFile.createNewFile();
System.out.println("full path = " + outFile.getAbsolutePath());
} else {
System.out.println("full path = " + outFile.getAbsolutePath());
}
FileOutputStream outStream = new FileOutputStream(outFile);

outStream.write(data);
outStream.close();
input.close();
}
} catch (Exception e) {
e.printStackTrace();
}

return new ModelAndView("welcome");
}

❸ java web前端上傳文件到後台常用的幾種方式

1、使用form表單提交
但是這里要記得添加enctype屬性,這個屬性是指定form表單在向伺服器提交之前,對表單數據如何進行編碼。 文件域中的name="file"屬性的值,需要和後台接收的對象名一致,不然接收不到。
2、使用ajax提交文件
使用ajax提交首先引入jquery-form.js文件才能實現,接著使用上面的html代碼,加入以js則可以實現ajax提交文件。
3、使用FormData對象
4、後台接收文件,框架採用的Spring Boot 微服務框架,因為該框架搭建很方便所以採用這個框架寫例子。

❹ form java springmvc怎麼上傳文件

@Controller

@RequestMapping("/saveloads")

public class testload {

@RequestMapping("/index")

public ModelAndView index(){

ModelAndView model=new ModelAndView("/user/index");

return model;

}

@RequestMapping("/saveload")

public ModelAndView upload(@RequestParam(value="fileload")MultipartFile file){

ModelAndView model=new ModelAndView("/user/result");

//拿到文件的名字

String fileName=file.getOriginalFilename();

System.out.println(fileName);

//轉移目標

File tagetFile=new File("F:/image/",fileName);

try{

//將MUltipartfile轉移到tagetFile

file.transferTo(tagetFile);

}catch(IllegalStateException e){

e.printStackTrace();

}catch(IOException e){

e.printStackTrace();

}

model.addObject("fileUrl","/image/"+fileName);

return model;

}

}

寫一個有上傳功能的jsp頁面,action自己改吧,
然後在Tomcat伺服器中設置文件存儲的位置,要和代碼中寫的位置一樣,我寫的是F:/image/

❺ JAVA WEB文件上傳步驟

JAVA WEB文件上傳步驟如下:
實現 Web 開發中的文件上傳功能,兩個操作:在 Web 頁面添加上傳輸入項,在 Servlet 中讀取上傳文件的數據並保存在本地硬碟中。
1、Web 端上傳文件。在 Web 頁面中添加上傳輸入項:<input type="file"> 設置文件上傳輸入項時應注意:(1) 必須設置 input 輸入項的 name 屬性,否則瀏覽器將不會發送上傳文件的數據。(2) 必須把 form 的 enctype 屬性設為 multipart/form-data,設置該值後,瀏覽器在上傳文件時,將把文件數據附帶在 http 請求消息體中,並使用 MIME 協議對上傳文件進行描述,以方便接收方對上傳數據進行解析和處理。(3) 表單提交的方式要是 post
2、伺服器端獲取文件。如果提交表單的類型為 multipart/form-data 時,就不能採用傳統方式獲取數據。因為當表單類型為 multipart/form-data 時,瀏覽器會將數據以 MIME 協議的形式進行描述。如果想在伺服器端獲取數據,那麼我們必須採用獲取請求消息輸入流的方式來獲取數據。
3、Apache-Commons-fileupload。為了方便用戶處理上傳數據,Apache 提供了一個用來處理表單文件上傳的開源組建。使用 Commons-fileupload 需要 Commons-io 包的支持。
4、fileuplpad 組建工作流程
(1)客戶端將數據封裝在 request 對象中。
(2)伺服器端獲取到 request 對象。
(3)創建解析器工廠 DiskFileItemFactory 。
(4)創建解析器,將解析器工廠放入解析器構造函數中。之後解析器會對 request 進行解析。
(5)解析器會將每個表單項封裝為各自對應的 FileItem。
(6)判斷代表每個表單項的 FileItem 是否為普通表單項 isFormField,返回 true 為普通表單項。
(7)如果是普通表單項,通過 getFieldName 獲取表單項名,getString 獲得表單項值。
(8)如果 isFormField 返回 false 那麼是用戶要上傳的數據,可以通過 getInputStream 獲取上傳文件的數據。通過getName 可以獲取上傳的文件名。

❻ java表單提交裡面的文件上傳,用同步的方式好還是用非同步的好啊

你好,很高興回答你的問題。
這里做成非同步比較好。如果同步的話,遇到文件大的話,表單提交會很慢,體驗非常不好。
如果有幫助到你,請點擊採納。

❼ java實現文件上傳,代碼盡量簡潔~~~~~·

普通方法實現任意上傳?本地文件?本地文件直接用FileInputStream即可。
jspsmartupload需要在提交的form表單中添加一個屬性,具體內容忘了=。=

❽ Java中上傳文件和表單數據提交如何質蕕

//1.form表單
//註:上傳文件的表單,需要將form標簽設置enctype="multipart/form-data"屬性,意思是將Content-Type設置成multipart/form-data
<form action="xxx" method="post" enctype="multipart/form-data">
<input type="text" name="name" id="id1" /> <br />
<input type="password" name="password" /> <br />
<input type="file" name="file" value="選擇文件"/> <input id="submit_form" type="submit" value="提交"/>
</form>
//2.servlet實現文件接收的功能
boolean isMultipart = ServletFileUpload.isMultipartContent(request);//判斷是否是表單文件類型

DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload sfu = new ServletFileUpload(factory);
List items = sfu.parseRequest(request);//從request得到所有上傳域的列表
for(Iterator iter = items.iterator();iter.hasNext();){
FileItem fileitem =(FileItem) iter.next(); if(!fileitem.isFormField()&&fileitem!=null){
//判讀不是普通表單域即是file
System.out.println("name:"+fileitem.getName());
}
}

3.擴展一下springboot
@RequestMapping("/xxx")
@ResponseBody
public String handleFileUpload(@RequestParam("file") MultipartFile file) {
if (!file.isEmpty()) {
try {
BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream(new File(
file.getOriginalFilename())));
System.out.println(file.getName());
out.write(file.getBytes());
out.flush();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
return "上傳失敗," + e.getMessage();
} catch (IOException e) {
e.printStackTrace();
return "上傳失敗," + e.getMessage();
}
return "上傳成功";
} else {
return "上傳失敗,因為文件是空的.";
}
}

❾ java如何實現文件上傳

public static int transFile(InputStream in, OutputStream out, int fileSize) {
int receiveLen = 0;
final int bufSize = 1000;
try {
byte[] buf = new byte[bufSize];
int len = 0;
while(fileSize - receiveLen > bufSize)
{
len = in.read(buf);
out.write(buf, 0, len);
out.flush();
receiveLen += len;
System.out.println(len);
}
while(receiveLen < fileSize)
{
len = in.read(buf, 0, fileSize - receiveLen);
System.out.println(len);
out.write(buf, 0, len);
receiveLen += len;
out.flush();
}
} catch (IOException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
return receiveLen;
}
這個方法從InputStream中讀取內容,寫到OutputStream中。
那麼發送文件方,InputStream就是FileInputStream,OutputStream就是Socket.getOutputStream.
接受文件方,InputStream就是Socket.getInputStream,OutputStream就是FileOutputStream。
就OK了。 至於存到資料庫里嘛,Oracle里用Blob。搜索一下,也是一樣的。從Blob能獲取一個輸出流。

❿ java上傳文件時用戶不提交表單,上傳的文件怎麼刪除

給個建議,臨時目錄的文件其實可以非同步刪除 ,

就是說不需要依賴用戶是否提交表單這個行為。

整個服務里需要有那麼一個線程或者進程管理這個目錄

這個線程需要有2塊功能

  1. 定時偵測目錄的文件數/目錄空間

  2. 給一個閥值,當目錄文件數、空間到怎樣的值 ,按文件的創建時間排序刪除最老的文檔,刪除數量可以給定一個百分比,當然你可以做一些過濾排除(比如創建時間到現在時間差少於半小時的不刪除)

熱點內容
Android添加插件 發布:2024-10-27 06:14:33 瀏覽:154
x在c語言中是什麼意思 發布:2024-10-27 06:12:19 瀏覽:783
安卓手機怎麼關閉數據 發布:2024-10-27 06:08:59 瀏覽:514
安卓怎麼打出07 發布:2024-10-27 06:01:34 瀏覽:151
linux查看系統位數 發布:2024-10-27 05:59:19 瀏覽:604
魔獸該文件夾未包含正確的游戲版本 發布:2024-10-27 05:57:15 瀏覽:674
安卓手機如何唱k 發布:2024-10-27 05:55:40 瀏覽:883
新手適合什麼樣的安卓手機 發布:2024-10-27 05:46:46 瀏覽:218
兒童身高演算法 發布:2024-10-27 05:42:27 瀏覽:765
怎麼進qq加密空間 發布:2024-10-27 05:40:01 瀏覽:537