java中上傳代碼 上傳文件的代碼

java實(shí)現(xiàn)文件上傳,代碼盡量簡(jiǎn)潔~~~~~·

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

成都創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的達(dá)坂城網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

SmartUpload mySmartUpload = new SmartUpload();

//如果是struts2.0或者webwork 則是mySmartUpload.initialize(ServletActionContext.getServletConfig(),ServletActionContext.getRequest(),ServletActionContext.getResponse());

mySmartUpload.initialize(servlet.getServletConfig(), request,response);

mySmartUpload.setTotalMaxFileSize(500000);

//如果上傳任意文件不設(shè)置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);

java 文件上傳的代碼,盡量詳細(xì)一點(diǎn)。。。

// 這是我寫的一個(gè)方法,里面只需要傳兩個(gè)參數(shù)就OK了,在任何地方調(diào)用此方法都可以文件上傳

/**

* 上傳文件

* @param file待上傳的文件

* @param storePath待存儲(chǔ)的路徑(該路徑還包括文件名)

*/

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中怎么把文件上傳到服務(wù)器的指定路徑?

文件從本地到服務(wù)器的功能,其實(shí)是為了解決目前瀏覽器不支持獲取本地文件全路徑。不得已而想到上傳到服務(wù)器的固定目錄,從而方便項(xiàng)目獲取文件,進(jìn)而使程序支持EXCEL批量導(dǎo)入數(shù)據(jù)。

java中文件上傳到服務(wù)器的指定路徑的代碼:

在前臺(tái)界面中輸入:

form method="post" enctype="multipart/form-data" ?action="../manage/excelImport.do"

請(qǐng)選文件:input type="file" ?name="excelFile"

input type="submit" value="導(dǎo)入" onclick="return impExcel();"/

/form

action中獲取前臺(tái)傳來數(shù)據(jù)并保存

/**

* excel 導(dǎo)入文件

* @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);//保存到服務(wù)器的路徑

}

log.info("action:{} Method:{} end","usermanager","excelImport" );

return "";

}

/**

* 將MultipartFile轉(zhuǎn)化為file并保存到服務(wù)器上的某地

*/

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();

}

Java中fileupload上傳文件的代碼

private static DiskFileItemFactory factory; //獲得磁盤文件條目工廠

private static ServletFileUpload upload; //文件上傳處理類

factory = new DiskFileItemFactory(); //獲得磁盤文件條目工廠

factory.setRepository(new File(config.getCache())); //創(chuàng)建緩存工廠

factory.setSizeThreshold(1024*1024*2) ; //設(shè)置緩存區(qū)的大小

upload = new ServletFileUpload(factory); //高水平的API文件上傳處理

upload.setSizeMax(10 * 1024 * 1024); //設(shè)置文件上傳的最大值

upload.setFileSizeMax(2* 1024 * 1024); //設(shè)置文件上傳的最大值

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);

}

}

java上傳文件代碼

public class FileUpLoad extends ActionSupport{

//"多文件上傳就用list就可以了private ListFile file;"

private File file;

//上傳文本的name

public File getFile() {

return file;

}

public void setFile(File file) {

this.file = file;

}

private String fileContentType;

//上傳的文件類型。

public String getFileContentType() {

return fileContentType;

}

public void setFileContentType(String fileContentType) {

this.fileContentType = fileContentType;

}

//獲取上傳文件的名稱

private String fileFileName;

public String getFileFileName() {

return fileFileName;

}

public void setFileFileName(String fileFileName) {

this.fileFileName = fileFileName;

}

public String upload() throws Exception

{

//獲取文件上傳路徑

String root=ServletActionContext.getRequest().getRealPath("/upload");

InputStream is=new FileInputStream(file);

String.substring(fileFileName.indexOf("."));//截取上傳文件的后綴。便于新定義名稱。.jpg

System.out.println(name);

File descFile=new File(root,新定義的文件名稱+fileFileName.indexOf("."));

OutputStream os=new FileOutputStream(descFile);

byte[] buffer=new byte[1024];

int length=0;

while(-1!=(length=(is.read(buffer))))

{

os.write(buffer, 0, length);

}

is.close();

os.close();

return SUCCESS;

}

}

網(wǎng)站名稱:java中上傳代碼 上傳文件的代碼
文章鏈接:http://muchs.cn/article2/docccic.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)、網(wǎng)站設(shè)計(jì)公司、網(wǎng)站導(dǎo)航、用戶體驗(yàn)、定制開發(fā)、商城網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設(shè)