本文目錄一覽:
- 1、java實現圖片上傳至服務器並顯示,如何做?希望要具體的代碼實現
- 2、java 如何只通過後台把本地的圖片上傳的服務器上去?
- 3、大神們 java將圖片傳在另外一個服務器怎麼弄
- 4、java上傳圖片到服務器指定路徑
- 5、java上傳圖片到遠程服務器上,怎麼解決呢?
java實現圖片上傳至服務器並顯示,如何做?希望要具體的代碼實現
很簡單。
可以手寫IO讀寫(有點麻煩)。
怕麻煩的話使用FileUpload組件 在servlet里doPost嵌入一下代碼
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
response.setContentType(“text/html;charset=gb2312”);
PrintWriter out=response.getWriter();
//設置保存上傳文件的目錄
String uploadDir =getServletContext().getRealPath(“/up”);
System.out.println(uploadDir);
if (uploadDir == null)
{
out.println(“無法訪問存儲目錄!”);
return;
}
//根據路徑創建一個文件
File fUploadDir = new File(uploadDir);
if(!fUploadDir.exists()){
if(!fUploadDir.mkdir())//如果UP目錄不存在 創建一個 不能創建輸出…
{
out.println(“無法創建存儲目錄!”);
return;
}
}
if (!DiskFileUpload.isMultipartContent(request))
{
out.println(“只能處理multipart/form-data類型的數據!”);
return ;
}
DiskFileUpload fu = new DiskFileUpload();
//最多上傳200M數據
fu.setSizeMax(1024 * 1024 * 200);
//超過1M的字段數據採用臨時文件緩存
fu.setSizeThreshold(1024 * 1024);
//採用默認的臨時文件存儲位置
//fu.setRepositoryPath(…);
//設置上傳的普通字段的名稱和文件字段的文件名所採用的字符集編碼
fu.setHeaderEncoding(“gb2312”);
//得到所有表單字段對象的集合
List fileItems = null;
try
{
fileItems = fu.parseRequest(request);//解析request對象中上傳的文件
}
catch (FileUploadException e)
{
out.println(“解析數據時出現如下問題:”);
e.printStackTrace(out);
return;
}
//處理每個表單字段
Iterator i = fileItems.iterator();
while (i.hasNext())
{
FileItem fi = (FileItem) i.next();
if (fi.isFormField()){
String content = fi.getString(“GB2312”);
String fieldName = fi.getFieldName();
request.setAttribute(fieldName,content);
}else{
try
{
String pathSrc = fi.getName();
if(pathSrc.trim().equals(“”)){
continue;
}
int start = pathSrc.lastIndexOf(‘\\’);
String fileName = pathSrc.substring(start + 1);
File pathDest = new File(uploadDir, fileName);
fi.write(pathDest);
String fieldName = fi.getFieldName();
request.setAttribute(fieldName, fileName);
}catch (Exception e){
out.println(“存儲文件時出現如下問題:”);
e.printStackTrace(out);
return;
}
finally //總是立即刪除保存表單字段內容的臨時文件
{
fi.delete();
}
}
}
注意 JSP頁面的form要加enctype=”multipart/form-data” 屬性, 提交的時候要向服務器說明一下 此頁面包含文件。
如果 還是麻煩,乾脆使用Struts 的上傳組件 他對FileUpload又做了封裝,使用起來更傻瓜化,很容易掌握。
—————————–
以上回答,如有不明白可以聯繫我。
java 如何只通過後台把本地的圖片上傳的服務器上去?
這裡你弄錯了一個問題;\x0d\x0a你的程序是要傳遞圖片的二進制數據.\x0d\x0a而不是傳遞路徑,然後再到服務器讀取文件數據(你的服務器有這個文件?)\x0d\x0a只有當你的服務器下有這個文件了,你傳遞一個路徑,讀取是可以的.\x0d\x0a//—\x0d\x0a關於如何上傳文件, 自己google一下,很多教程
大神們 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);
}
java上傳圖片到服務器指定路徑
private File myFile; //文件
private String myFileContentType; //類型
private String myFileFileName; //文件名
//。。。。getXXX() setXXX()方法
//輸入流
InputStream is = new FileInputStream(myFile);
//設定文件路徑
String photoPath = ServletActionContext.getServletContext()
.getRealPath(“/user/photo/”);
File filePhotoPath = new File(photoPath);
//判斷這個路徑是否存在,如果不存在創建這個路徑
if (!filePhotoPath.isDirectory()) {
filePhotoPath.mkdir();
}
String extension = FilenameUtils.getExtension(this
.getMyFileFileName()); //後綴名 比如jpg
String filename = UUID.randomUUID().toString() + “.” + extension;
// 目標文件
File tofile = new File(photoPath, filename);
// 輸出流
OutputStream os = new FileOutputStream(tofile);
byte[] buffer = new byte[1024];
int length = 0;
while ((length = is.read(buffer)) 0) {
os.write(buffer, 0, length);
}
// 關閉輸入流
is.close();
// 關閉輸出流
os.close();
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:admin@10.0.0.1/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();}
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/249020.html