java上傳檔案到 linux 下的 tomcat 檔案伺服器
阿新 • • 發佈:2018-11-19
linux 安裝 tomcat 伺服器,我安裝的是 tomcat 7,未測試其他版本
linux 安裝 tomcat 連線 https://mp.csdn.net/mdeditor/83987640#
本次使用的阿里雲 linux os7 , tomcat 7 + ssm
1、修改目錄 tomcat 下的 conf 下的 web.xml 為非只讀
<init-param> <param-name>readonly</param-name> <param-value>false</param-value> </init-param>
vi 不太熟練,直接使用 WinScp 遠端工程修改了
2、maven依賴新增,我之前使用tomcat9 和springboot 內建tomcat無法建立Jersey客戶端,大家注意依賴
<!-- 檔案上傳到外網 tomcat依賴 --> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-client</artifactId> <version>1.2</version> </dependency> <!--tomcat7以上 無法建立Jersey客戶端依賴 --> <dependency> <groupId>org.glassfish.jersey.bundles</groupId> <artifactId>jaxrs-ri</artifactId> <version>2.4</version> </dependency> <!-- mvc 檔案上傳依賴 --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <!-- json 依賴--> <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> <type>jar</type> <classifier>jdk15</classifier> <scope>compile</scope> </dependency>
3、MVC 檔案上傳配置,沒配置的新增一下,不然controller 接收不到檔案
<!-- 檔案上傳 單位位元組--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="1024000" /> <property name="maxInMemorySize" value="4096" /> <property name="defaultEncoding" value="UTF-8"></property> </bean>
4、controller 檔案上傳方法
//===================檔案上傳到 linux下tomcat伺服器======================
@RequestMapping("/fileupload")
public void uploadPic(HttpServletRequest request, PrintWriter out, String lastRealPath) throws IOException {
//強制轉換request
MultipartHttpServletRequest mr = (MultipartHttpServletRequest) request;
//從表單獲得檔案
//獲得檔案型別input的name
Iterator<String> iter = mr.getFileNames();
String inputName = iter.next();
//獲得檔案
MultipartFile mf = mr.getFile(inputName);
byte[] mfs = mf.getBytes();
//定義上傳後的檔名字
String fileName = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
Random random = new Random();
for(int i = 0; i < 3; i++){
fileName = fileName + random.nextInt(10);
}
//獲得字尾名
String oriFileName = mf.getOriginalFilename();
String suffix = oriFileName.substring(oriFileName.lastIndexOf("."));
//要上傳檔案的絕對路徑
String realPath ="http://47.107.128.84:8080/" +"file/test/"+fileName + suffix;
//相對路徑,回寫頁面
String relativePath = "file/test/"+fileName + suffix;
//由於我們需要在不同主機上上傳不能直接通過流的方式寫檔案
//建立Jersey客戶端,tomcat7 支援,tomcat9 需新增依賴
Client client = Client.create();
//判斷是不是第一次上傳
if(StringUtils.isNotBlank(lastRealPath)){
WebResource wr = client.resource(lastRealPath);
wr.delete();
}
//指定要上傳的具體的地址,引數就是要上傳的圖片的絕對路徑
WebResource wr = client.resource(realPath);
//檔案的上傳到檔案主機上
wr.put(mfs);
//使用json物件把絕對路徑和相對路徑寫回去
JSONObject jo = new JSONObject();
jo.accumulate("realPath", realPath);
jo.accumulate("relativePath", relativePath);
//{"realPath":"http://...", "relativePath":"/upload/.."}
String result = jo.toString();
out.write(result);
}
外加、富文字檔案上傳方法
//=================富文字檔案上傳到 linux下tomcat伺服器======================
@RequestMapping("/uploadForFck.do")
public void uploadForFck(HttpServletRequest request, PrintWriter out) throws IOException{
//強制轉換request
MultipartHttpServletRequest mr = (MultipartHttpServletRequest) request;
//從表單獲得檔案
//獲得檔案型別input的name
Iterator<String> iter = mr.getFileNames();
String inputName = iter.next();
//獲得檔案
MultipartFile mf = mr.getFile(inputName);
byte[] mfs = mf.getBytes();
//定義上傳後的檔名字
String fileName = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
Random random = new Random();
for(int i = 0; i < 3; i++){
fileName = fileName + random.nextInt(10);
}
//獲得字尾名
String oriFileName = mf.getOriginalFilename();
String suffix = oriFileName.substring(oriFileName.lastIndexOf("."));
//要上傳檔案的絕對路徑
String realPath ="http://47.107.128.84:8080/" +"file/test/"+fileName + suffix;
//相對路徑,回寫頁面
String relativePath = "file/test/"+fileName + suffix;
//由於我們需要在不同主機上上傳不能直接通過流的方式寫檔案
//建立Jersey客戶端
Client client = Client.create();
//指定要上傳的具體的地址,引數就是要上傳的圖片的絕對路徑
WebResource wr = client.resource(realPath);
//檔案的上傳到檔案主機上
wr.put(mfs);
}
前臺from
<form action="file/fileupload" method="post" enctype="multipart/form-data">
<label>檔案上傳</label>
<input type="file" name="file">
<input type="submit" value="提交">
</form>
winscp檢視上傳結果,檔案以已經上傳上去了