導航:首頁 > IDC知識 > java實現上傳圖片伺服器

java實現上傳圖片伺服器

發布時間:2020-12-05 08:02:11

1、java上傳圖片到遠程伺服器上,怎麼解決呢?

需要這樣的一個包 jcifs-1.1.11
public static void forcdt(String dir){
InputStream in = null;
OutputStream out = null;
File localFile = new File(dir);
try{
//創建file類 傳入本地文件路徑
//獲得本地文件的名字
String fileName = localFile.getName();
//將本地文件的名字和遠程目錄的名字拼接在一起
//確保上傳後的文件於本地文件名字相同
SmbFile remoteFile = new SmbFile("smb://administrator:[email protected]/e$/aa/");
//創建讀取緩沖流把本地的文件與程序連接在一起
in = new BufferedInputStream(new FileInputStream(localFile));
//創建一個寫出緩沖流(注意jcifs-1.3.15.jar包 類名為Smb開頭的類為控制遠程共享計算機"io"包)
//將遠程的文件路徑傳入SmbFileOutputStream中 並用 緩沖流套接
out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile+"/"+fileName));
//創建中轉位元組數組
byte[] buffer = new byte[1024];
while(in.read(buffer)!=-1){//in對象的read方法返回-1為 文件以讀取完畢
out.write(buffer);
buffer = new byte[1024];
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
//注意用完操作io對象的方法後關閉這些資源,走則 造成文件上傳失敗等問題。!
out.close();
in.close();
}catch(Exception e){
e.printStackTrace();}
}
}

2、java 中如何向伺服器上傳圖片

我們使用一些已有的組件幫助我們實現這種上傳功能。
常用的上傳組件:
Apache 的 Commons FileUpload
JavaZoom的UploadBean
jspSmartUpload
以下,以FileUpload為例講解
1、在jsp端
<form id="form1" name="form1" method="post" action="servlet/fileServlet" enctype="multipart/form-data">
要注意enctype="multipart/form-data"
然後只需要放置一個file控制項,並執行submit操作即可
<input name="file" type="file" size="20" >
<input type="submit" name="submit" value="提交" >
2、web端
核心代碼如下:
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
try {
List items = upload.parseRequest(request);
Iterator itr = items.iterator();
while (itr.hasNext()) {
FileItem item = (FileItem) itr.next();
if (item.isFormField()) {
System.out.println("表單參數名:" + item.getFieldName() + ",表單參數值:" + item.getString("UTF-8"));
} else {
if (item.getName() != null && !item.getName().equals("")) {
System.out.println("上傳文件的大小:" + item.getSize());
System.out.println("上傳文件的類型:" + item.getContentType());
System.out.println("上傳文件的名稱:" + item.getName());
File tempFile = new File(item.getName());
File file = new File(sc.getRealPath("/") + savePath, tempFile.getName());
item.write(file);
request.setAttribute("upload.message", "上傳文件成功!");
}else{
request.setAttribute("upload.message", "沒有選擇上傳文件!");
}
}
}
}catch(FileUploadException e){
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
request.setAttribute("upload.message", "上傳文件失敗!");
}
request.getRequestDispatcher("/uploadResult.jsp").forward(request, response);
}

3、java 如何只通過後台把本地的圖片上傳的伺服器上去

import java.io.*;
public class CopyIMG{
    public static void main(String[] args)throws Exception{
        File file = new File("C:xx.jpg");
        if(!file.exists())
            throw new RuntimeException("文件不存在..");
        FileInputStream fis = new FileInputStream(file);
        byte[] b = new byte[1024];
        int len = 0;
        FileOutputStream fos = new FileOutStream("要保存的伺服器路徑");
        while((len=is.read(b))!=-1){
            fos.write(b,0,len);
        }
        fos.close();
        fis.close();
    }
}

4、java 求jsp上傳圖片到伺服器代碼

提交頁面表單
<form action="up.jsp" enctype="multipart/form-data" method="post">
<input type="file" name="file">
<input type="submit" value="確定">
</form>
上傳頁面up.jsp
<%@page import="java.io.FileWriter"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
import="java.io.*"
pageEncoding="UTF-8"%>
<%
/**
協議頭四行內容
45 -----------------------------7de231211204c4
80 Content-Disposition: form-data; name="file"; filename="xx.txt"
26 Content-Type: text/plain
2
標記文件結尾
-----------------------------7de231211204c4--
**/
ServletInputStream sin = request.getInputStream();
byte[] buffer = new byte[1024 * 8];
int length = 0, row = 0;
String mark = "";
String filename = "";
while ((length = sin.readLine(buffer, 0, buffer.length)) > 0) {
out.println(length + " " + new String(buffer, 0, length, "UTF-8") + "<br>");
String s = new String(buffer, 0, length, "UTF-8");
if (row == 0)
mark = s.trim();
else if (s.indexOf("filename=") > 0) {
int end = s.lastIndexOf("\"");
int start = s.substring(0, end).lastIndexOf("\"");
filename = s.substring(start + 1, end);
} else if ("".equals(s.trim()))
break;
row ++;
}

out.println("filename: " + filename + "<br>");
filename = request.getRealPath("/") + "../" + filename;
FileOutputStream fout = new FileOutputStream(filename);
while ((length = sin.readLine(buffer, 0, buffer.length)) > 0) {
String s = new String(buffer, 0, length);
if (s.startsWith(mark))
break;
fout.write(buffer, 0, length);
}
fout.flush();
fout.close();
File f = new File(filename);
out.println(f.exists());
out.println(f.getAbsolutePath());
%>

5、java實現圖片上傳至伺服器並顯示,如何做?希望要具體的代碼實現

其實就是個update語句,你把圖片的路徑保存到欄位裡面,jsp頁面這么寫 <img src="你資料庫保存的圖片路徑回就行了" /> 不懂答的話 我QQ [email protected] 你加過來 我給你回答 或者你留下你的QQ 我幫你寫

6、java 如何只通過後台把本地的圖片上傳的伺服器上去?

這里你弄錯copy了一個問題;
你的程序是要傳遞圖片的二進制數據.
而不是傳遞路徑,然後再到伺服器讀取文件數據(你的伺服器有這個文件?)
只有當你的伺服器下有這個文件了,你傳遞一個路徑,讀取是可以的.
//---
關於如何上傳文件, 自己google一下,很多教程

7、java實現圖片上傳至伺服器並顯示,如何做?希望要具體的代碼實現

?

8、java中怎麼把文件上傳到伺服器的指定路徑?

文件從本地到伺服器的功能,其實是為了解決目前瀏覽器不支持獲取本地文件全路徑。不得已而想到上傳到伺服器的固定目錄,從而方便項目獲取文件,進而使程序支持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();

}

9、java中怎麼將本地圖片上傳到伺服器上

伺服器端,做一個FTP,客戶端使用APACHE的FTP組件上傳。。。。。。。



伺服器端實現HTTP的POST接收,,,,,客戶端使用httpclient組件post上去

與java實現上傳圖片伺服器相關的知識