當前位置:首頁 » 文件管理 » java上傳代碼嗎

java上傳代碼嗎

發布時間: 2022-12-06 07:27:41

java 文件上傳的代碼,盡量詳細一點。。。

// 這是我寫的一個方法,裡面只需要傳兩個參數就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上傳文件代碼

public class FileUpLoad extends ActionSupport{

//"多文件上傳就用list就可以了private List<File> 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;

}

}

③ java圖片批量上傳代碼

用struts也可以實現 多文件上傳
下面是我寫的代碼,作為參考!

/*文件目錄*/
public static String [] fileArray={
"logo.png",
"index.swf",
"OEMInfo.txt",
"favicon.ico"};

/**
* @author Caoshun
* @see 接收並保存文件
* */
public static void receiveAndSaveAllFileByPath(ActionForm form,String rootPath1,String rootPath2){
String fileName="";
//獲取表單中的文件資源
Hashtable<Object, Object> files = form.getMultipartRequestHandler().getFileElements();
//遍歷文件,並且循環保存
//當前處理文件序號
int file_num=1;
for (Enumeration<Object> e = files.keys(); e.hasMoreElements();) {

/*根據處理的當前文件下標,確定文件名*/
fileName=fileArray[file_num-1];

FormFile file = (FormFile) files.get((String) e.nextElement());
if (file != null && file.getFileSize() > 0) {
try {
//使用formfile.getInputStream()來獲取一個文件的輸入流進行保存。
//文件名
//String fileName = file.getFileName();
//System.out.println("debug in AddEnterpriceAction.java on line 152 fileName is : "+fileName);
//文件大小
//int fileSize = file.getFileSize();
//文件流
InputStream is = file.getInputStream();
//將輸入流保存到文件
//String rootPath = this.servlet.getServletContext().getRealPath("files");

//往cn中寫入
File rf = new File(rootPath1);
FileOutputStream fos = null;
fos = new FileOutputStream(new File(rf, fileName));
byte[] b = new byte[10240];
int real = 0;
real = is.read(b);
while (real > 0) {
fos.write(b, 0, real);
real = is.read(b);
}

//往en中寫入
File rf2 = new File(rootPath2);
InputStream is2 = file.getInputStream();
FileOutputStream fos2 = null;
fos2 = new FileOutputStream(new File(rf2, fileName));
byte[] b2 = new byte[10240];
int real2 = 0;
real2 = is2.read(b2);
while (real2 > 0) {
fos2.write(b2, 0, real2);
real2 = is2.read(b2);
}

//關閉文件流
fos.close();
is.close();
fos2.close();
is2.close();
} catch (RuntimeException e1) {
e1.printStackTrace();
} catch (Exception ee) {
ee.printStackTrace();
}
file.destroy();

}
file_num++;
}
}

④ 如何用java代碼實現ftp文件上傳

import java.io.File;
import java.io.FileInputStream;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

public class test {

private FTPClient ftp;
/**
*
* @param path 上傳到ftp伺服器哪個路徑下
* @param addr 地址
* @param port 埠號
* @param username 用戶名
* @param password 密碼
* @return
* @throws Exception
*/
private boolean connect(String path,String addr,int port,String username,String password) throws Exception {
boolean result = false;
ftp = new FTPClient();
int reply;
ftp.connect(addr,port);
ftp.login(username,password);
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return result;
}
ftp.changeWorkingDirectory(path);
result = true;
return result;
}
/**
*
* @param file 上傳的文件或文件夾
* @throws Exception
*/
private void upload(File file) throws Exception{
if(file.isDirectory()){
ftp.makeDirectory(file.getName());
ftp.changeWorkingDirectory(file.getName());
String[] files = file.list();
for (int i = 0; i < files.length; i++) {
File file1 = new File(file.getPath()+"\\"+files[i] );
if(file1.isDirectory()){
upload(file1);
ftp.changeToParentDirectory();
}else{
File file2 = new File(file.getPath()+"\\"+files[i]);

⑤ 求JAVA上傳圖片代碼

package com;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import com.jspsmart.upload.*;

public class uploadfiles extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
//使用了一個第三方的組件,存放在web-inf/lib下
response.setContentType("text/html;charset=GB2312");

//由於SmartUpload的初始化方法需要pageContext,所以我們在servlet中得到他
//為了得到pageConext要首先得到JspFactory的實例
//通過JspFactory的實例的getPageContext方法得到pageConext的實例
JspFactory jf = null;

//得到JspFactory的實例
jf=JspFactory.getDefaultFactory();

/*
getPageContext(Servlet servlet,
ServletRequest request,
ServletResponse response,
java.lang.String errorPageURL,
boolean needsSession,
int buffer,
boolean autoflush)
*/
PageContext pageContext=jf.getPageContext(this,request,response,null,true,8192,true);

try
{
//實例化SmartUpload
SmartUpload mySmartUpload=new SmartUpload();

//初始化SmartUpload的實例,需要PageContext的實例
mySmartUpload.initialize(pageContext);

//設定最大上傳的位元組數,其實可以不進行設定,表示上傳的文件沒有大小限制
//mySmartUpload.setTotalMaxFileSize(10000000);
mySmartUpload.upload();

//下面是單文件上傳
//上傳的文件以com.jspsmart.upload.File 代表,如果文件名稱重復,則進行覆蓋
com.jspsmart.upload.File file=mySmartUpload.getFiles().getFile(0);
String upLoadFileName=file.getFileName();

//調用com.jspsmart.upload.File實例的saveas的方法保存文件,此時的文件名即是
//保存到伺服器上的文件名
file.saveAs("/upload/"+upLoadFileName);
Request req =
Text t = .....;
t.setUpload(upLoadFileName);
t.set.....(req);
}
catch(SmartUploadException e)
{
System.out.println(e.getMessage());
}

}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException
{
doGet(request,response);
}
}

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

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

熱點內容
得力文件夾5302 發布:2024-04-26 00:21:32 瀏覽:90
您的個人文件夾 發布:2024-04-26 00:03:12 瀏覽:67
睿雲伺服器功能介紹 發布:2024-04-25 23:59:51 瀏覽:570
標致5008怎麼連接安卓 發布:2024-04-25 23:25:08 瀏覽:793
安卓下載管理器哪個好 發布:2024-04-25 23:22:48 瀏覽:442
考試系統源碼php 發布:2024-04-25 23:09:46 瀏覽:136
磁碟禁止訪問 發布:2024-04-25 22:53:48 瀏覽:288
多線程ftp上傳 發布:2024-04-25 22:41:36 瀏覽:115
phpqrcode 發布:2024-04-25 22:41:36 瀏覽:33
桂平上網密碼是多少 發布:2024-04-25 22:32:10 瀏覽:575