你說的2種方法都是很簡單的,參考網上的資料都不難做出,用io流做更是基礎中的基礎,我說下smartupload好了,有的人是直接寫在jsp上面,感覺比較亂,我一般都是寫在action里面,打好jar包和配置后

成都創(chuàng)新互聯(lián)服務項目包括玉州網站建設、玉州網站制作、玉州網頁制作以及玉州網絡營銷策劃等。多年來,我們專注于互聯(lián)網行業(yè),利用自身積累的技術優(yōu)勢、行業(yè)經驗、深度合作伙伴關系等,向廣大中小型企業(yè)、政府機構等提供互聯(lián)網行業(yè)的解決方案,玉州網站推廣取得了明顯的社會效益與經濟效益。目前,我們服務的客戶以成都為中心已經輻射到玉州省份的部分城市,未來相信會繼續(xù)擴大服務區(qū)域并繼續(xù)獲得客戶的支持與信任!
SmartUpload mySmartUpload = new SmartUpload();
//如果是struts2.0或者webwork 則是mySmartUpload.initialize(ServletActionContext.getServletConfig(),ServletActionContext.getRequest(),ServletActionContext.getResponse());
mySmartUpload.initialize(servlet.getServletConfig(), request,response);
mySmartUpload.setTotalMaxFileSize(500000);
//如果上傳任意文件不設置mySmartUpload.setAllowedFilesList(文件后綴名)就可以了
mySmartUpload.upload();
for (int i = 0; i mySmartUpload.getFiles().getCount(); i++) {
com.jspsmart.upload.File file = mySmartUpload.getFiles().getFile(i);
if (file.isMissing()) continue;
file.saveAs(保存的地址 + file.getFileName(),
su.SAVE_PHYSICAL);
private static DiskFileItemFactory factory; //獲得磁盤文件條目工廠
private static ServletFileUpload upload; //文件上傳處理類
factory = new DiskFileItemFactory(); //獲得磁盤文件條目工廠
factory.setRepository(new File(config.getCache())); //創(chuàng)建緩存工廠
factory.setSizeThreshold(1024*1024*2) ; //設置緩存區(qū)的大小
upload = new ServletFileUpload(factory); //高水平的API文件上傳處理
upload.setSizeMax(10 * 1024 * 1024); //設置文件上傳的最大值
upload.setFileSizeMax(2* 1024 * 1024); //設置文件上傳的最大值
ListFileItem list = upload.parseRequest(request);
for(FileItem item : list){
String fieldName = item.getFieldName(); //獲取表單的屬性名字
String fileName = item.getName() ; //獲取文件名
if(item.isFormField()){ //如果獲取的 表單信息是普通的 文本 信息
}else{
File file = new File("d://test.txt");
item.write(file);
}
}
structs
的
jsp
頁面文件上傳表單,只要項目是SSH的就行了
jsp:
s:form
action="add.do"
id="inputForm"
enctype="multipart/form-data"
td
s:file
name="upload"
cssClass="{required:true}"
contenteditable="false"/s:file
span
class="field_tipinfo"請選擇文件/span
/td
/s:form
action:
private
File
upload;//上傳的文件
....
public
String
add()
throws
Exception
{
//保存文件
save(upload);
}
...
文件從本地到服務器的功能,其實是為了解決目前瀏覽器不支持獲取本地文件全路徑。不得已而想到上傳到服務器的固定目錄,從而方便項目獲取文件,進而使程序支持EXCEL批量導入數據。
java中文件上傳到服務器的指定路徑的代碼:
在前臺界面中輸入:
form method="post" enctype="multipart/form-data" ?action="../manage/excelImport.do"
請選文件:input type="file" ?name="excelFile"
input type="submit" value="導入" onclick="return impExcel();"/
/form
action中獲取前臺傳來數據并保存
/**
* excel 導入文件
* @return
* @throws IOException
*/
@RequestMapping("/usermanager/excelImport.do")
public String excelImport(
String filePath,
MultipartFile ?excelFile,HttpServletRequest request) throws IOException{
log.info("action:{} Method:{} start","usermanager","excelImport" );
if (excelFile != null){
String filename=excelFile.getOriginalFilename();
String a=request.getRealPath("u/cms/www/201509");
SaveFileFromInputStream(excelFile.getInputStream(),request.getRealPath("u/cms/www/201509"),filename);//保存到服務器的路徑
}
log.info("action:{} Method:{} end","usermanager","excelImport" );
return "";
}
/**
* 將MultipartFile轉化為file并保存到服務器上的某地
*/
public void SaveFileFromInputStream(InputStream stream,String path,String savefile) throws IOException
{ ? ?
FileOutputStream fs=new FileOutputStream( path + "/"+ savefile);
System.out.println("------------"+path + "/"+ savefile);
byte[] buffer =new byte[1024*1024];
int bytesum = 0;
int byteread = 0;
while ((byteread=stream.read(buffer))!=-1)
{
bytesum+=byteread;
fs.write(buffer,0,byteread);
fs.flush();
}
fs.close();
stream.close();
}
// 這是我寫的一個方法,里面只需要傳兩個參數就OK了,在任何地方調用此方法都可以文件上傳
/**
* 上傳文件
* @param file待上傳的文件
* @param storePath待存儲的路徑(該路徑還包括文件名)
*/
public void uploadFormFile(FormFile file,String storePath)throws Exception{
// 開始上傳
InputStream is =null;
OutputStream os =null;
try {
is = file.getInputStream();
os = new FileOutputStream(storePath);
int bytes = 0;
byte[] buffer = new byte[8192];
while ((bytes = is.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytes);
}
os.close();
is.close();
} catch (Exception e) {
throw e;
}
finally{
if(os!=null){
try{
os.close();
os=null;
}catch(Exception e1){
;
}
}
if(is!=null){
try{
is.close();
is=null;
}catch(Exception e1){
;
}
}
}
}
Java代碼實現文件上傳
FormFile?file=manform.getFile();?
String?newfileName?=?null;
String?newpathname=null;
String?fileAddre="/numUp";
try?{
InputStream?stream?=?file.getInputStream();//?把文件讀入
String?filePath?=?request.getRealPath(fileAddre);//取系統(tǒng)當前路徑
File?file1?=?new?File(filePath);//添加了自動創(chuàng)建目錄的功能
((File)?file1).mkdir();???
newfileName?=?System.currentTimeMillis()
+?file.getFileName().substring(
file.getFileName().lastIndexOf('.'));
ByteArrayOutputStream?baos?=?new?ByteArrayOutputStream();
OutputStream?bos?=?new?FileOutputStream(filePath?+?"/"
+?newfileName);
newpathname=filePath+"/"+newfileName;
System.out.println(newpathname);
//?建立一個上傳文件的輸出流
System.out.println(filePath+"/"+file.getFileName());
int?bytesRead?=?0;
byte[]?buffer?=?new?byte[8192];
while?((bytesRead?=?stream.read(buffer,?0,?8192))?!=?-1)?{
bos.write(buffer,?0,?bytesRead);//?將文件寫入服務器
}
bos.close();
stream.close();
}?catch?(FileNotFoundException?e)?{
e.printStackTrace();
}?catch?(IOException?e)?{
e.printStackTrace();
}
當前題目:java完整文件上傳代碼 java完整文件上傳代碼是什么
標題來源:http://chinadenli.net/article38/hpjgpp.html
成都網站建設公司_創(chuàng)新互聯(lián),為您提供響應式網站、微信小程序、網站策劃、小程序開發(fā)、App設計、用戶體驗
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)