Java ファイルをアップロードコード

サンプルコード:

/**
*
* @param upload アップロードファイル
* @param reaName  名前
* @param uploadType タイプ
* @return
* @throws IOException
*/
public String uploadFile(File upload, String reaName,
String uploadType) throws IOException {
String uploadpath="D:/startnews24/csg";
String uploadpath2="/upload/image/";
String fileDir = uploadpath +uploadpath2;
OutputStream os = null;
InputStream is = null;
File file=new File(fileDir);
if(!file.exists()){
file.mkdirs();
}
try {
is = new FileInputStream(upload);
os = new FileOutputStream(fileDir + reaName);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = is.read(buffer, 0, 8192)) != -1)
os.write(buffer, 0, bytesRead);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (os != null) {
os.close();
}
if (is != null) {
is.close();
}
}
return uploadpath2+reaName;
}

Source

Posted by arkgame