當前位置:首頁 » 文件管理 » ssh上傳圖片代碼

ssh上傳圖片代碼

發布時間: 2022-09-21 22:07:21

A. 怎麼把自己電腦上的文件上傳到ssh伺服器上

由於使用ssh,我們無法使用簡單的拖拽或者復制粘貼操作對本地計算機和ssh伺服器上的文件進行交流。我們需要用到scp命令行。
程序代碼
scp <本地文件名> <用戶名>@<ssh伺服器地址>:<上傳保存路徑即文件名>
舉例以說明:
如果希望將當前目錄下的a.txt文件上傳到ssh伺服器上test文件夾並改名為b.txt,其中ssh伺服器網址為127.0.0.1,用戶名admin。
代碼如下:
程序代碼 scp a.txt [email protected]:/home/neo/test/b.txt

下載也很簡單,只需要將「本地文件名」和後面伺服器的信息對調即可。

B. ssh 上傳圖片中的Action怎麼寫

package com.file.action;

import java.io.File;

import org.apache.commons.io.FileUtils;

import com.opensymphony.xwork2.ActionSupport;

public class FileUploadAction extends ActionSupport {
private File file;
private String contentType;
private String fileName;

@Override
public String execute() throws Exception {
File saveFile = new File("c:/download/" + fileName);
if (!saveFile.getParentFile().exists())
saveFile.getParentFile().mkdirs();
FileUtils.File(file, saveFile);// 復制文件
this.addFieldError("isSuccess", "文件上傳成功!");
return SUCCESS;
}

public void setUploadContentType(String contentType) {
this.contentType = contentType;
}

public void setUpload(File file) {
this.file = file;
}

public void setUploadFileName(String fileName) {
this.fileName = fileName;
}
}

具體代碼請看:

C. JAVA,SSH怎麼獲取圖片上傳時間

第一種,Java代碼獲取當前系統時間。
Date date =new Date();
SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
Sting time = dateFormat.format(date);

第二種,sql語句直接插入當前時間。
instre into table values(now())
第三種,建表時候將時間欄位默認是設置成 CURRENT_TIMESTAMP
例如: create table usertable(
`id` int(10) NOT NULL AUTO_INCREMENT,

`name` varchar(24) NOT NULL,

`createTime` datetime default CURRENT_TIMESTAMP
)
插入數據的時候,只需要寫入 name 即可。id、createtime都自動生成數據。
insert into usertable (name) values("張三");
·
·

D. ssh2使用fileuoload上傳圖片提示問題!上傳成功了但不知怎麼提示上傳圖片過大!

int i = file.length();大小

webroot路徑 reqeust.getRealPath("/") + "goods"+File.separator + "image"

E. 誰有SSH上傳下載文件的代碼

上傳:

package com.wb.ekeng.web.action.file;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessages;

import com.wb.ekeng.ebo.BO_File;
import com.wb.ekeng.info.INFO_Admin;
import com.wb.ekeng.info.INFO_File;
import com.wb.ekeng.web.action.BaseAction;
import com.wb.ekeng.web.filemanage.File;
import com.wb.ekeng.web.filemanage.SmartUpload;
import com.wb.ekeng.web.util.Convertor;

public class ACT_AddFile extends BaseAction {

public ACT_AddFile() {
super();
}

public ActionForward doExcute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response, ActionMessages errors) throws Exception {
//文件保存路徑
String strSavePath ="/upload/file/";
//允許上傳的文件類型
String strAllowExt ="jpg,jpeg,gif,bmp,txt,java";
//允許上傳最大位元組數
int maxSize =1048576;
//上傳文件
SmartUpload upload=new SmartUpload();
upload.initialize(this.getServlet().getServletConfig(), request, response);
upload.upload("UTF-8");
//獲取文件
String[] allowExtList =strAllowExt.split(",");
File fileBuffer =upload.getFiles().getFile(0);
if(fileBuffer.isMissing()){
throw new Exception("error.act.act_addfile.error3");
}

HttpSession session=request.getSession();
INFO_Admin loginAdmin=(INFO_Admin) session.getAttribute("loginadmin");
Long lAdminId=loginAdmin.getId();

String strName=upload.getRequest().getParameter("name");
//如果遇見上傳中文文件出現亂碼問題,上一句可以改成這樣(其他語句參照這): String strName=new String(upload.getRequest().getParameter("name").getBytes(),"UTF-8");
String strNeedPoint=upload.getRequest().getParameter("needpoint");
String strType=upload.getRequest().getParameter("type");
String strDes=upload.getRequest().getParameter("des");

Integer iNeedPoint=null;

//驗證參數
if(strName==null||strNeedPoint==null||strType==null||strDes==null){
throw new Exception("error.common.badrequest");
}
this.doValidate(errors,INFO_File.validateName(strName));
this.doValidate(errors,INFO_File.validateNeedPoint(strNeedPoint));
this.doValidate(errors,INFO_File.validateType(strType));
this.doValidate(errors,INFO_File.validateDes(strDes));

//驗證文件大小
int FileSize=fileBuffer.getSize();
if(FileSize>maxSize){
this.doValidate(errors,"error.act.act_addfile.error2");
}

//驗證文件類型
String strFileExt =fileBuffer.getFileExt();
boolean flag =false;
for(int i=0;i<allowExtList.length;i++) {
if(allowExtList[i].toLowerCase().equals(strFileExt.toLowerCase())){
//找到了匹配的後綴
flag=true;
}
}
if(strFileExt.equals("") || flag ==false){
this.doValidate(errors,"error.act.act_addfile.error1");
}

if(!errors.isEmpty()){
System.out.println(errors.toString());
return null;
}
//參數轉換
strName =Convertor.convertHalfToFull(strName);
iNeedPoint=new Integer(strNeedPoint);
Integer iFileSize=Integer.valueOf(FileSize);

//構造saveName
String strSaveName=lAdminId+"_"+BO_File.getNowString()+"."+upload.getFiles().getFile(0).getFileExt();
fileBuffer.saveAs(strSavePath + strSaveName);

//提交數據
BO_File boFile=new BO_File();
boFile.addFile(strName,strType,strDes,strSaveName,iNeedPoint,lAdminId,iFileSize);
return new ActionForward("/admin/main/download/admindownload.do",true);
}
}

下載:
package com.wb.ekeng.web.action.file;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessages;

import com.wb.ekeng.web.action.BaseAction;
import com.wb.ekeng.ebo.BO_File;
import com.wb.ekeng.info.INFO_File;
import com.wb.ekeng.info.INFO_User;

import com.wb.ekeng.web.filemanage.SmartUpload;

/**
* 備注:
* 文件下載Action
* 輸入:
* String fileid
* String userid
* 輸出:
*/
public class ACT_Download extends BaseAction {

public ACT_Download() {
super();
}
public ActionForward doExcute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response, ActionMessages errors) throws Exception {
HttpSession session =request.getSession();
//獲取參數
Long lFileId=null;
String fileId =request.getParameter("fileid");
INFO_User infoUser=(INFO_User)session.getAttribute("loginuser");

//驗證參數
if(fileId==null || infoUser.getId() ==null){
throw new Exception("error.common.badrequest");
}
lFileId=new Long(fileId);
BO_File boFile=new BO_File();
INFO_File infoFile =null;

//判斷用戶是否為第一次下載
接收存儲下載資源Id的session,然後判斷要下載的資源Id是否沒存在於當前登錄的下載頁表中(即是否是第一次下載),如果是則調用下載扣除積分方法,並把這個資源的Id存入列表中。如果不是,則把標記位置false,直接下載資源,不調用扣除積分的方法。
boolean isFirstDownLoad =true;
ArrayList downFileList =(ArrayList)session.getAttribute("downfilelist");
for(int index =0; index <downFileList.size(); index ++){
Long lFileIdBuffer =(Long)downFileList.get(index);
if(lFileIdBuffer.longValue() ==lFileId.longValue()){
isFirstDownLoad =false;
break;
}
}
if(isFirstDownLoad){
infoFile=boFile.download(lFileId,infoUser.getId());
downFileList.add(lFileId);
}else{
infoFile=boFile.getFile(lFileId);
}
(下面就是有關下載的代碼)
//新建一個SmartUpload對象
SmartUpload su = new SmartUpload();
//初始化
su.initialize(this.getServlet().getServletConfig(), request, response);
//設定contentDisposition為null以禁止瀏覽器自動打開文件,
//保證點擊鏈接後是下載文件。若不設定,則下載的文件擴展名為
//doc時,瀏覽器將自動用word打開它。擴展名為pdf時,
//瀏覽器將用acrobat打開。
su.setContentDisposition(null);
//下載文件
su.downloadFile("/upload/file/"+infoFile.getSaveName());
return mapping.findForward("success");
}}

F. ssh怎麼把本地的文件上傳到伺服器

由於使用ssh,我們無法使用簡單的拖拽或者復制粘貼操作對本地計算機和ssh伺服器上的文件進行交流。我們需要用到scp命令行。
程序代碼
scp <本地文件名> <用戶名>@<ssh伺服器地址>:<上傳保存路徑即文件名>
舉例以說明:
如果希望將當前目錄下的a.txt文件上傳到ssh伺服器上test文件夾並改名為b.txt,其中ssh伺服器網址為127.0.0.1,用戶名admin。
代碼如下:
程序代碼 scp a.txt [email protected]:/home/neo/test/b.txt

下載也很簡單,只需要將「本地文件名」和後面伺服器的信息對調即可

G. 使用java ssh框架 利用Uploadify做上傳功能,後台action里該怎麼寫,請寫出具體代碼

Uploadify 是一個比較坑的東西,得用servlet, web.xml里配置 然後servlet接收
<servlet>
<servlet-name>upload</servlet-name>
<servlet-class>com.action.upload</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>upload</servlet-name>
<url-pattern>/upload/FileUploadServlet</url-pattern>
</servlet-mapping>
servlet里代碼

public void doGetAndPost(HttpServletRequest request,
HttpServletResponse response){

try {
request.setCharacterEncoding("utf-8");
} catch (UnsupportedEncodingException e) {
logger.error("後台添加圖片,request設置編碼符失敗! {}",e.getMessage());
}
DiskFileItemFactory fac = new DiskFileItemFactory();

ServletFileUpload upload = new ServletFileUpload(fac);

upload.setHeaderEncoding("utf-8");

List fileList = null;

try {
fileList = upload.parseRequest(request);
} catch (FileUploadException ex) {
ex.printStackTrace();
return;
}
String imageName = null;
Iterator<FileItem> it = fileList.iterator();

while (it.hasNext()) {

FileItem item = it.next();

if (!item.isFormField()) {
Random r = new Random();
int rannum = (int) (r.nextDouble() * (9999 - 1000 + 1)) + 1000;
imageName=getNowStrDate() + rannum;

FileBean fileBean = new FileBean();
fileBean.setFileName(item.getName());
fileBean.setFileExtension(item.getName().substring(item.getName().indexOf(".")+1));
FileBean flbn;
try {
//文件伺服器處理上傳圖片
flbn = FastDFSUtil.upload(fileBean, item.getInputStream());
} catch (IOException e) {
logger.info("添加圖片,上傳文件伺服器失敗!",e);
}

}
}
}

H. 如何通過ssh上傳文件到伺服器

需要用到scp命令行。
scp <本地文件名> <用戶名>@<ssh伺服器地址>:<上傳保存路徑即文件名> /*註:必須在未登錄伺服器的情況下*/
舉例以說明:
如果希望將當前目錄下的a.txt文件上傳到ssh伺服器上test文件夾並改名為b.txt,其中ssh伺服器網址為127.0.0.1,用戶名admin。
代碼如下:
程序代碼 scp a.txt [email protected]:/home/neo/test/b.txt

下載也很簡單,只需要將「本地文件名」和後面伺服器的信息對調即可

熱點內容
存儲過程修改記錄 發布:2024-10-10 08:23:28 瀏覽:58
呱呱編程 發布:2024-10-10 08:12:54 瀏覽:894
androidoa 發布:2024-10-10 08:07:14 瀏覽:894
安卓手機怎麼關掉開了的游戲 發布:2024-10-10 07:50:14 瀏覽:681
idea新建java類 發布:2024-10-10 07:50:12 瀏覽:70
教務處的賬號和密碼是什麼 發布:2024-10-10 07:47:51 瀏覽:790
php種子怎麼下載 發布:2024-10-10 07:39:28 瀏覽:660
陰陽師防禦隊伍怎麼配置 發布:2024-10-10 07:19:52 瀏覽:888
雲存儲測試工具 發布:2024-10-10 07:19:03 瀏覽:466
java的組件 發布:2024-10-10 06:58:30 瀏覽:176